2016-11-18 14 views
1

のCodeIgniterでオートコンプリートの検索フィールドで、結果は私はCodeIgniterのを使用してオートコンプリートの検索フィールドを作っ

({"destination":"Accra"},{"destination":"Lagos"}) 

のような配列に来ていると私は、HTMLに変換したいです入力フィールドのドロップダウンに表示されます。私はいろいろな方法を試みましたが、成功することができました。親切に私を助けてください。

ビュー:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Welcome to CodeIgniter</title> 


</head> 
<body> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script type="text/javascript" src="js/script.js"></script> 
<script> 
$(document).ready(function(){ 
    $('#country_id').keyup(function() { 
     var min_length = 0; 
    var keyword = $('#country_id').val(); 
    if (keyword.length >= min_length) { 
     $.ajax({ 
      url: 'http://localhost/new/index.php/travels/search_fields', 
      type: 'POST', 
      data: { term: $("#country_id").val()}, 
      success:function(data){ 
       $('#country_list_id').show(); 
       $('#country_list_id').html(data); 
      } 
     }); 
    } else { 
     $('#country_list_id').hide(); 
    } 
}); 
}); 
</script> 

<form action="" method="post"> 
<input type="text" name="country_id" id="country_id" onKeyUp="do_search();" > 

    <ul id="country_list_id"> </ul>     
</form> 

</body> 
</html> 

モデル:

public function search_field($country_id){ 
    $this->db->select("destination"); 
    $this->db->from('travels_detail'); 
    $this->db->like('destination', $country_id); 
    $query = $this->db->get(); 
    return json_encode($query->result_array()); 
} 

コントローラー:

public function search_fields(){ 
     $this->load->helper('form'); 
    $country_id = $this->input->post('term'); 
    $data['var']= $this->travel->search_field($country_id); 
    echo $data['var']; 
} 
+0

なぜsearch_fields()にreturn文がないのですか? –

答えて

0

あなたのデータであればここで

は私のビュー、モデルとコントローラのコードですから大丈夫になっていますサーバーを(jsonとして)試してリストを作成してください:

+0

コントローラーに入りますか? –

+0

@ハリスキーン、あなたのajax 'success'では実際にはありません。 – DontVoteMeDown

+0

そのコードに誤りを与えます: –

関連する問題