2017-05-28 64 views
0

我試圖讓房屋被填充在第一個下拉列表中選擇的第二個下拉列表中。我在第二個下拉列表中找不到結果。從屬下拉選擇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; 
} 
+0

因爲您在'fill_houses'中忘記了'echo'? –

+1

'$ query'是一個字符串,你不能在其上調用'result'方法。 – PeterMader

+0

而js中的'get_houses'對'val'變量一無所知。 –

回答

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'); 
    } 
}); 

} 
+0

謝謝@AdhershMNair ...它的工作 – Lexie

+0

你是歡迎@Lexie – AdhershMNair

+0

heyy,我再次,爲什麼它不張貼到'