ajax依赖的dropdown codeigniter

时间:2017-06-05 05:01:16

标签: javascript ajax codeigniter

我想使用提供给控制器的ajax从索引中获取 id ,并且控制器将使用json输出类别ID 并提供给索引文件。

控制器


            public function index() {
                 $this->load->model('sales/Sales_model');
                 $this->load->model('product/Product_model');

                 $user_info['p_info'] = $this->Product_model->fetch_data();
                 $this->load->view('sales/index',$user_info);

            }

            function get_pcatagory(){

                 $this->load->model('sales/Sales_model');
                 $pid = $this->input->post('pro_id');

                 $cat_info = $this->Sales_model->fetch_data_pid($pid);

            if(count($cat_info) > 0){
                $select_box = '';
                $select_box .= 'Select Catagory ID';
               foreach ($cat_info as $value) {
                    // $value->id data will go to option of the select menu
                }
               echo json_encode($select_box);
            }
        }

模型

`function fetch_data_pid($pid){
        $query = $this->db->get_where('product',array('id'=>$pid));
        return $query->result();
    }`

的Ajax

`

    $(document).ready(function(){
        $('#pid').on('change',function(){

            var pro_id_data = $('#pid').val();
            alert(pro_id_data);

            $.ajax({
                    url:'sales/get_pcatagory',
                    type:'POST',
                    data:{'pro_id' : pro_id_data},
                    dataType: 'json',
                    success:   
                    function(result){
                        $('#cate').html(result);
                    },
                    error: 
                    function(){
                         alert('error')
                    }
            });
        });
    });



`

查看

<div class="form-group">
            <label>Product ID</label>
            <select name="p_id" id="pid" class="form-control" placeholder="Enter Product"  required>
                <option value="0">Select Product ID</option>
                 <?php
                     foreach ($p_info as $value) {
                 ?>
                <option value="<?php echo $value->id ;?>"><?php echo $value->p_name ;?></option>
                <?php }?>
             </select>
        </div>

       <div id="" class="form-group">
            <label>Category</label>
            <select name="catagory" id="cate" class="form-control" placeholder="Enter Product" required >
             </select>
        </div>

但它警告错误。我该如何解决?

0 个答案:

没有答案