dtxpz8785 2016-05-04 10:18
浏览 24

codeigniter复选框过滤

hi i want to checkbox filtering in codeigniter... but it shows not correctly output...if anyone knows this please try to solve this

this is my controller

<?php  
class Check extends CI_Controller {
  public function laptops(){
    $this->load->model('check_m');
    $filter = array(
        'price' => $this->input->get('price'),
        'name' =>$this->input->get('name')
    );
    $data['laptop'] = $this->check_m->laptops_m($filter);

   // echo json_encode( $data['laptop'] );
   $this->load->view('check_view',$data);
 }
}
?>

Model:

<?php
class check_m extends CI_Model {
  function laptops_m($filter = null){
    $this->db->select('*')
             ->from('mobile_phones');
    // $query = $this->db->get('laptop_notebook')->result();
    // return $query;
    if($filter['name']){
      $this->db->where('name', $filter['name']);
    }
    if($filter['price']){
      $this->db->where('price', $filter['price']);
    }
    $query = $this->db->get()->result();
    return $query;
  }  
}
?>

View

<input type="checkbox" name="name" value="acer" class="searcType">      
<input type="checkbox" name="name" value="lenovo">    
<input type="checkbox" name="price" value="1000">   
<table>
  <tbody>
    <?php foreach ($laptop as $laptops_all) { ?>
      <tr>
        <td><p>Laptop <?php echo $laptops_all->name ?> </p></td>
      </tr>
    <?php } ?>
  </tbody>
</table>  

<script>  
  $('.searchType').click(function() {
    alert($(this).attr('id'));  //-->this will alert id of checked checkbox.
    if(this.checked){
      $.ajax({
        url: localhost/code/check/laptops,
        dataType: 'json',
        success: function(data){
          $.each(data, function(index, element) {
            $("tbody").empty();
            $("tbody").append("<tr><td>"+
              "Laptop "+element.brand+""+
              "</td></tr>");
            });
          }
      }); 
    }
  });
</script>

Here i want to filter the items with checkboxes... but i didn't get filtering output....

  • 写回答

1条回答 默认 最新

  • doushu2699 2016-05-04 19:37
    关注

    You are not sending any parameters for $this->input->get() to pick up on.

    If you are just sending a GET request, you won't need to send a csrf_token like someone mentioned in the comments.


    You may need to check/set a few config variable's first. These are in application/config/config.php

    $config['uri_protocol'] = 'QUERY_STRING'; // OR AUTO
    $config['allow_get_array'] = TRUE;
    $config['enable_query_strings'] = TRUE;
    

    var BASEHREF = "<?php echo base_url();?>";
    

    Your ajax options should then include the following

    {
       url  : BASEHREF + 'check/laptops',
       type : 'GET',
       data : { name : 'toshiba', price : '1000'}
    }
    // GET http://yoursite.com/check/laptops?name=toshiba&price=1000
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题