jQuery 自动建议

发布于 2024-12-11 08:31:15 字数 1016 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尽揽少女心 2024-12-18 08:31:15

您将 MySQL 查询的结果拉入数组 $data 中,然后继续循环编辑该数组。试试这个:

$data = array();
$qu = new MySQL();
$qu->rQuery("SELECT id,name FROM cities WHERE name LIKE '$input%'");
$rows = $qu->getRows();
foreach($rows as $a){
    $json = array();
    $json['value'] = $a['id'];
    $json['name'] = $a['name'];
    $data[] = $json;
}    
header("Content-type: application/json");
echo json_encode($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:

$data = array();
$qu = new MySQL();
$qu->rQuery("SELECT id,name FROM cities WHERE name LIKE '$input%'");
$rows = $qu->getRows();
foreach($rows as $a){
    $json = array();
    $json['value'] = $a['id'];
    $json['name'] = $a['name'];
    $data[] = $json;
}    
header("Content-type: application/json");
echo json_encode($data);

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.

寂寞花火° 2024-12-18 08:31:15

在客户端使用脚本的完整路径:

<script>
$(document).ready(function(){
    $("#postna").autoSuggest("http://yoursite.com/get.php");
})
</script>

Use in the client side the full path to your script:

<script>
$(document).ready(function(){
    $("#postna").autoSuggest("http://yoursite.com/get.php");
})
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文