搜索栏中的自动完成功能使用codeigniter
问题描述:
每一个。我正在尝试进行自动填充搜索,但它无法正常工作。 当用户在搜索栏中写入数据时,数据通过表中的一列进行。 这里是代码: 查看:搜索栏中的自动完成功能使用codeigniter
<!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>
$("#country_id").autocomplete({
source: function(request, response) {
$.ajax({
url: "http://localhost/new/index.php/travels/search_fields",
data: { term: $("#country_id").val()},
dataType: "json",
type: "POST",
success: function(data){
var resp = $.map(data,function(obj){
return obj.tag;
});
response(resp);
}
});
},
minLength: 2
});
</script>
<form>
<input type="text" name="country_id" id="country_id" >
</form>
</body>
</html>
型号:
public function search_field($country_id){
$q= $this->db->query("select()->from('travels_detail')->where('destination', $country_id");
echo json_encode($q->result_array());
}
控制器:
public function search_fields(){
$destination= $this->input->post('country_id');
$this->travel->search_field($country_id);
}
答
在模型中的活动查询是错误的$this->db->query("select()->from('travels_detail')->where('destination', $country_id");
,和你都在积极错过->get()
查询。
关注娄这样的:
public function search_field($country_id){
$q = $this->db->select("*")->from('travels_detail')->where('destination', $country_id)->get();
echo json_encode($q->result_array());
}
,或者
public function search_field($country_id){
$this->db->select("*");
$this->db->from('travels_detail');
$this->db->where('destination', $country_id);
$q = $this->db->get();
echo json_encode($q->result_array());
}
在控制器代替$destination
到$country_id
您在AJAX调用这样data: { term: $("#country_id").val()},
term
岗位价值,但你在控制器调用$this->input->post('country_id');
因此,在控制器$country_id = $this->input->post('term');
所以,你的控制器:
public function search_fields(){
$country_id = $this->input->post('term');
$this->travel->search_field($country_id);
}
是啊,我改变了模式,但它显示空数组 –
我已经编辑我的答案。在控制器而不是'$ destination'到'$ country_id' –
我编辑了我的答案,请刷新你的页面。 –