通过查询数据库帮助使用 ajax 自动完成
我想到的问题是我的查询是否从数据库返回值,或者我是否遗漏了代码中的任何内容。我一直在试图弄清楚,但我无法理解。
print_r($tagnames) 不会在屏幕上打印任何内容。
这段代码是控制器的一部分,
function get_Names() {
// Convert the string to the lowercase
$q = strtolower($this->input->post('q', TRUE));
if(!$q) {
return;
}
$tagnames[] = $this->autocomplete_model->getData();
print_r($tagnames);
echo json_encode($tagnames);
}
我有 autocomplete_model.php ,这段代码是模型的一部分
class Autocomplete_model extends CI_Model {
public function __construct() {
parent::__construct();
}
function getData() {
$q = $this->db->query("SELECT * FROM tags");
if($q->num_rows() > 0) {
foreach($q->result() as $row) {
$data[] = $row->tag_name;
}
return $data;
}
}
}
,我认为有以下代码
$(document).ready(function() {
$(function() {
$( "#tagname" ).autocomplete({
source: function(request, response) {
$.ajax({ url: "<?php echo site_url('generator/get_Names'); ?>",
data: { term: $("#tagname").val()},
dataType: "json",
type: "POST",
success: function(data){
response(data);
}
});
},
minLength: 2
});
});
});
提前致谢。
The question what comes in my mind is whether my query is returning the values from the database or did I miss anything in my code. I have been trying to figure it out but I am unable to understand.
print_r($tagnames) doesn't print anything on the screen.
This code is part of the controller
function get_Names() {
// Convert the string to the lowercase
$q = strtolower($this->input->post('q', TRUE));
if(!$q) {
return;
}
$tagnames[] = $this->autocomplete_model->getData();
print_r($tagnames);
echo json_encode($tagnames);
}
I have autocomplete_model.php and this code is part of the model
class Autocomplete_model extends CI_Model {
public function __construct() {
parent::__construct();
}
function getData() {
$q = $this->db->query("SELECT * FROM tags");
if($q->num_rows() > 0) {
foreach($q->result() as $row) {
$data[] = $row->tag_name;
}
return $data;
}
}
}
I have the following code in my view
$(document).ready(function() {
$(function() {
$( "#tagname" ).autocomplete({
source: function(request, response) {
$.ajax({ url: "<?php echo site_url('generator/get_Names'); ?>",
data: { term: $("#tagname").val()},
dataType: "json",
type: "POST",
success: function(data){
response(data);
}
});
},
minLength: 2
});
});
});
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的 AJAX 调用正在发送“term”作为发布数据,但您的 get_Names() 方法正在寻找“q”的发布参数。改变其中之一,我想你应该会很好。
It looks like your AJAX call is sending "term" as post data but your get_Names() method is looking for a post parameter of "q". Change one of them and I think you should be good.