jQuery 自动建议
我对 jQuery 还很陌生,所以我需要一些帮助: 我刚刚开始使用 jQuery Autosuggest 插件,因为我真的会帮助我的网站。我下载了所有文件,将它们链接到我的网站,并使用它来将输入设置为自动建议:
<script>
$(document).ready(function(){
$("#postna").autoSuggest("get.php");
})
</script>
现在这部分工作正常,它改变了我的输入框的样式。对于 get.php 文件,我几乎从插件站点复制了它,但我更改了它,这样它会更好地为我服务:
<?php
require_once 'includes/mysql.class.php';
$input = $_GET['q'];
$data = array();
$qu = new MySQL();
$qu->rQuery("SELECT id,name FROM cities WHERE name LIKE '$input%'");
$data = $qu->getRows();
foreach($data as $a){
$json = array();
$json['value'] = $a['id'];
$json['name'] = $a['name'];
$data[] = $json;
}
header("Content-type: application/json");
echo json_encode($data);
?>
现在的问题是,当我输入文本时,它返回“未找到结果”,但是如果我转到 /get.php?q=velenje (velenje 是一个城市的名称),它呼应了这一点:
{"id":"681","name":"Velenje"}
对我可能做错的事情有什么建议吗?
I am pretty new to jQuery, so I need some help here:
I just started using jQuery Autosuggest Plugin, because I would really help my site. I downloaded all of the files, linked them to my site, and used this, to set an input as autosuggest:
<script>
$(document).ready(function(){
$("#postna").autoSuggest("get.php");
})
</script>
Now this part works fine, it changes the style of my input box. And for get.php file, I pretty much copied it from the plugin site, but I changed it, so it would serve me better:
<?php
require_once 'includes/mysql.class.php';
$input = $_GET['q'];
$data = array();
$qu = new MySQL();
$qu->rQuery("SELECT id,name FROM cities WHERE name LIKE '$input%'");
$data = $qu->getRows();
foreach($data as $a){
$json = array();
$json['value'] = $a['id'];
$json['name'] = $a['name'];
$data[] = $json;
}
header("Content-type: application/json");
echo json_encode($data);
?>
The problem now is, that when I input my text it returns "No results found", but if I go to /get.php?q=velenje (velenje is a name of a city), it echoes this:
{"id":"681","name":"Velenje"}
Any suggestion on what I might be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将 MySQL 查询的结果拉入数组
$data
中,然后继续循环编辑该数组。试试这个:编辑:我忘了注意,您似乎没有清理您的输入... Little Bobby Tables 向我们展示了为什么我们不应该这样做。
You're pulling the results of your MySQL query into an array
$data
and then proceeding to edit that array in a loop. Try this:EDIT: I forgot to note that you don't appear to be sanitizing your inputs... Little Bobby Tables shows us why we shouldn't do that.
在客户端使用脚本的完整路径:
Use in the client side the full path to your script: