【问题标题】:I want to click the checkbox to display the database filed values in codeigniter我想单击复选框以在 codeigniter 中显示数据库字段值
【发布时间】:2016-05-02 12:17:04
【问题描述】:

我想点击复选框以显示我想通过 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>");
                });
            }
        }); 

【问题讨论】:

    标签: php codeigniter mysqli


    【解决方案1】:

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

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

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

    数据

    类型:PlainObject 或 String 或 Array

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

    所以你应该遍历检查过的checkboxes 并将它的值添加到数组中。然后通过ajax请求的data属性发送这个数组。

    【讨论】:

    • 请任何人回答我
    • 正如我所说的,JS 方面存在问题,而不是 PHP 方面。我已经告诉过你应该怎么做。
    • 如何纠正这个问题
    【解决方案2】:

    试试 this..add 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);
        }
    

    【讨论】:

    • 控制器中的加载视图在哪里
    • 不工作相同的输出,它一次显示所有内容在单击复选框后不显示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 2020-07-03
    • 1970-01-01
    相关资源
    最近更新 更多