依赖下拉选择使用AJAX的codeigniter

时间:2017-05-30 07:17:40

标签: javascript php ajax codeigniter

我试图在第一次下拉菜单中选择的第二次下拉菜单中填写房屋。我在第二次下拉中没有得到任何结果。如果我转储结果如var_dump($estates->house_number);我得house number G3意味着有一些结果。但现在这不会与<option class="text-uppercase" value="' . $estates->house_id . '"> ' . $estates->house_number . '</option>;一起发送到视图

这是HTML:

<div class="col-sm-4 col-xs-12">
   <div class="form-group drop-custum">
       <select id="estate" name="estate" data-live-search="true" class="form-control show-tick" onchange="get_houses(this.value)">
          <option value="">-- Estate --</option>
          <?php
           $sql = $this->db->query("select * from estates ORDER BY estate_name asc");
           $result = $sql->result();
           foreach ($result as $estates):
          ?>
            <option class="text-uppercase" value="<?= $estates->estate_name?>"> <?= $estates->estate_name?></option>
         <?php endforeach; ?>
        </select>
    </div>
</div>
<div class="col-sm-4 col-xs-12">
    <div class="form-group drop-custum">
        <select name="house" data-live-search="true" id="house" class="form-control show-tick">
          <option value="">-- House --</option>
        </select>
    </div>
</div>

这是AJAX:

function get_houses()
{
 $.ajax({
    url:"fill_houses/",
    type:"POST",
    data:'estate_name='+val,
    success:function(data)
    {
        $("#house").html(data);
        alert('success');
    }
 }); 
}

这是PHP:

public function fill_houses()
{
    $query = $this->db->query("select * from houses where estate_name='" . $this->input->post('estate_name', true) . "' order by house_number asc ");
    $result = $query->result();

    foreach ($result as $estates) {
        var_dump($estates->house_number);

        //dump returns house number G3, meaning results are there

        $rslt =  '<option class="text-uppercase" value="' . $estates->house_id . '"> ' . $estates->house_number . '</option>';
        return $rslt;
        //this return doestnt populate the view for <select name="house"  id="thishouse"></select>
    }

}

1 个答案:

答案 0 :(得分:0)

检查这个ajax函数:

function get_houses(val)
{
   $.ajax({
      url:"<?php echo base_url(); ?>fill_houses",
      type:"POST",
      dataType:"json",
      data:'estate_name='+val,
      success:function(data)
      {
         $("#house").html(data);
         alert('success');
      }
   });

}   

控制器:

public function fill_houses()
{
$query = $this->db->query("select * from houses where estate_name='" . $this->input->post('estate_name', true) . "' order by house_number asc ");
$result = $query->result();

foreach ($result as $estates) {
    var_dump($estates->house_number);

    //dump returns house number G3, meaning results are there

    $rslt[] = '<option class="text-uppercase" value="'.$estates['house_id'].'">'.$estates['house_number'].'</option>';

    //this return doestnt populate the view for <select name="house"  id="thishouse"></select>
}
echo json_encode($rslt);

}