依赖下拉选择codeigniter Ajax

时间:2017-05-28 15:58:11

标签: javascript php ajax codeigniter

我试图在第一次下拉菜单中选择的第二次下拉菜单中填写房屋。我在第二次下拉中没有得到任何结果。

这是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 = "select * from houses where estate_name='" . $_POST["estate_name"] . "' order by house_number asc ";
    $result = $query->result();
    foreach ($result as $estates):
        '<option class="text-uppercase" value="'.$estates->house_id.'"> '.$estates->house_number.'</option>';
    endforeach;
}

1 个答案:

答案 0 :(得分:0)

您必须在php函数fill_houses()中返回该值。

public function fill_houses()
{

    $query = $this->db->query("select * from houses where estate_name='" . $_POST["estate_name"] . "' order by house_number asc ");
    $result = $query->result();
    foreach ($result as $estates):
      return  '<option class="text-uppercase" value="'.$estates->house_id.'"> '.$estates->house_number.'</option>';
    endforeach;
}

同样在javascript ajax函数get_houses()中你需要获取变量。和几个编辑。

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

}