[英]jQuery UI autocomplete in codeIgniter with sql, php
我正在嘗試使用jquery創建文本框自動完成功能。 但是,當我在調試器中檢查它時,出了點問題,它顯示了每個值,但未在視圖頁面的文本框下拉列表中顯示。 我是新手,所有幫助將不勝感激。
這是我的查看代碼。
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript">
$(function(){
$("#location").autocomplete({
source: 'set_location'
});
});
</script>
<form action= "#">
<table>
<tr>
<td><label for='location'>Location</label></td>
<td><input id="location" type="text" size="50" placeholder="Enter a location" name= "location"> </td>
</tr>
</table>
</form>
這是我的控制器代碼
if(isset($_GET['term'])){
$location = strtolower($_GET['term']);
$query = $this->venues_model->set_location($location);
echo $query;
}
這是我的模型代碼
$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('venue_details');
if(count($query->result_array()) > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlentities(stripslashes($row['location']));
}
echo json_encode($row_set);
}
嘗試這個,
if(isset($_GET['term'])){
$location = strtolower($_GET['term']);
echo $this->venues_model->set_location($location);
}
在模型中
function set_location() {
$row_set = array();
$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('venue_details');
if (count($query->result_array()) > 0) {
foreach ($query->result_array() as $row) {
$row_set[] = htmlentities(stripslashes($row['location']));
}
}
return json_encode($row_set);
}
還有這個
source: '<?= base_url("controller/set_location") ?>'
你必須return
從模型文件,並echo
來自您的控制器
模型
$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('venue_details');
if(count($query->result_array()) > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlentities(stripslashes($row['location']));
}
return json_encode($row_set);// return from model
}
控制者
if(isset($_GET['term'])){
$location = strtolower($_GET['term']);
echo $query = $this->venues_model->set_location($location);
}
並改變你的道路
source: '<?php echo base_url();?>index.php/controller/function'
只需將ui-widget
添加到您的div中即可。 確保引用jquery ui
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.