我想点击复选框来显示代码点火器中的数据库字段值


I want to click the checkbox to display the database filed values in codeigniter

我想点击复选框来显示我想要通过ajax调用获取的数据,但它正在显示数据库所有数据自动显示问题所在请找出并帮助我(只是我想要ajax调用)

控制器

//This is my controller
public function laptops()
{
$this->load->model('feature_model');
$filter = array(
    'price' => $this->input->get('price'),
    'name' =>$this->input->get('name')
);
$data['laptop'] = $this->feature_model->laptops_m($filter);
//echo json_encode($this->feature_model->laptops_m($filter));
$this->load->view('feature/checkbox',$data);
} 
//This is my 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;
}
//This is my view
<input type="checkbox" name="name" value="acer">  

<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>  

Ajax脚本:

//  This is the ajax script function
<script>
$("input[checkbox]").change(function(){
               $.ajax({
            url: localhost/ci35/feature/laptops,
            dataType: 'json',
            success: function(data){
                $.each(data, function(index, element) {
                    $("tbody").empty();
                    $("tbody").append("<tr><td>"+
                    "Laptop "+element.brand+""+
                    "</td></tr>");
                });
            }
        }); 

您尝试从这里的GET数组中获取要显示的数据:

$filter = array(
    'price' => $this->input->get('price'),
    'name' =>$this->input->get('name')
); 

但是您的GET数组是空的,因为您没有在json请求中发送任何数据。来自jQuery文档:

数据

类型:PlainObject或字符串或数组

要发送到的数据服务器它被转换为查询字符串(如果不是字符串的话)。它被附加到GET请求的url中。请参阅processData选项以阻止这种自动处理。对象必须是键/值对。如果value是一个数组,jQuery用同一个键序列化多个值基于传统设置的值(如下所述)。

所以您应该对已检查的checkboxes进行迭代,并将其值添加到数组中。然后通过ajax请求的data属性发送此数组。

试试这个。。将class="searchType"添加到html复选框元素中,然后在jquery 中

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

当您通过成功的ajax调用打印数据时,无需在视图中再次使用foreach。

同样在您的控制器中,您必须打印json数据。

    public function laptops()
    {
    $this->load->model('feature_model');
    $filter = array(
        'price' => $this->input->get('price'),
        'name' =>$this->input->get('name')
    );
    $data['laptop'] = $this->feature_model->laptops_m($filter);
    echo json_encode( $data['laptop'] );
   // $this->load->view('feature/checkbox',$data);
    }