使用codeigniter通过ajax显示数据

时间:2015-10-02 06:55:31

标签: php ajax codeigniter

我是ajax的新手.did无法理解我如何获得所需的输出。 我实现了依赖下拉。问题是,当我从下拉区域选择区域时,我无法显示该区域的餐厅。请告诉我如何获得餐厅的观点。

这是我的观看代码

<div class="container"id restaurant>


        <table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%">
            <thread>
                <tr style="width: 56%;">
                    <th>
                        No.
                    </th>
                    <th style="">
                        Restaurant Names
                    </th>
                </tr>
            </thread>
            <tbody>
            <?php $i=1;foreach($result as $row){
                ?>
                <tr id="<?php echo $row->restaurant_id; ?>" class="res_id">
                    <th style="">
                        <?php echo  $i++; ?>

                    </th>
                    <th style="">
                        <?php echo $row->restaurant_name; ?>

                    </th>
                    <th style="width: 1%" >
                        <a href="<?php echo base_url();?>index.php/BulkRecipe_Controller/bulk_recipe/<?php echo $row->restaurant_id;?>"  class="btn btn-warning" <i class="glyphicon-edit"></i>See Menu</a>
                    </th>
                </tr>
            <?php } ?>
            </tbody>
        </table>
    </div>

我的控制器代码

     public function get_rests()
    {

        $cit_id = $this->input->post('cit_id');
        $area = $this->input->post('areaID');
        $where = array('city_id'=>$cit_id,'city_area_id'=>$area);

        $data = $this->bulk->select_record('restaurant',$where);
        echo $data = json_encode($data);
}

其脚本代码

function get_rests(){

        var city_id = $('#city_id').val();
        var area_id = $("#area_id").val();
        $.ajax({
            type: "POST",
            url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
            data: {cit_id: city_id,areaID: area_id},
            dataType: "text",
            cache:false,
            success:
                function(data){
                    $('#restaurant').html(data);
                }
        });
    }

其型号代码

function select_record($table, $where = NULL){

    $this->db->select();
    if($where)
        $this->db->where($where);
    $this->db->from($table);
    $query = $this->db->get();
    return $query->result();
}

选择区域后的前端视图

enter image description here

2 个答案:

答案 0 :(得分:0)

 public function get_rests()
 {

    $cit_id = $this->input->post('cit_id');
    $area = $this->input->post('areaID');
    $where = array('city_id'=>$cit_id,'city_area_id'=>$area);

    $data = $this->bulk->select_record('restaurant',$where);
    echo  json_encode($data); // you will get json encoded data in your ajax success then you can parse the json using jquery and get the detail
}

答案 1 :(得分:0)

尝试在控制器中创建数据的html并显示在下拉列表中。

 public function get_rests()
    {

        $cit_id = $this->input->post('cit_id');
        $area = $this->input->post('areaID');
        $where = array('city_id'=>$cit_id,'city_area_id'=>$area);

        $data = $this->bulk->select_record('restaurant',$where);
       $html = 'your result';
  echo $html;

}

现在在ajax部分你可以这样做;

function get_rests(){

        var city_id = $('#city_id').val();
        var area_id = $("#area_id").val();
        $.ajax({
            type: "POST",
            url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
            data: {cit_id: city_id,areaID: area_id},
            dataType: "text",
            cache:false,
            success:
                function(data){
                    // alert(data);
                    $('#restaurant').html(data);
                }
        });
    }

有关如何使用ajax选择和查看数据的更多信息,您可以查看this Link