自动完成显示所有条目,不执行任何搜索 [英] autocomplete shows all entries, Does not do any searching
问题描述
请帮助我... 我是新手!请告诉我该怎么办.
Please help me ... I'm a newbie! Please tell me what to do.
processed.php
processed.php
<?php
include_once('../../dbConnect.inc.php');
$sql="SELECT name FROM artist";
$artist = select($sql);
disconnect();
$output_items = array();
while($row = mysql_fetch_array($artist))
{
$results[] = array('label' => $row['name']);
}
echo json_encode($results);
?>
index.php
index.php
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#artist").autocomplete(
{
source: 'processed.php',
});
});
</script>
我有这个问题: http://jsbin.com/alaci5
推荐答案
自动完成功能需要来源(当指定URL来过滤结果时).
Autocomplete expects the source (when an URL is specified to filter out the results).
来自文档:
字符串:使用字符串时,自动完成"插件会期望 指向将返回JSON数据的URL资源的字符串.它可以 在同一主机上或在另一主机上(必须提供JSONP).这 自动填充插件不过滤结果,而是查询 字符串中添加了一个term字段,服务器端脚本应在其中 用于过滤结果.例如,如果source选项是 设置为" http://example.com ",并且用户键入foo(一个GET请求) 将被设为 http://example.com?term=foo .数据本身可以是 格式与上述本地数据相同.
String: When a string is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The Autocomplete plugin does not filter the results, instead a query string is added with a term field, which the server-side script should use for filtering the results. For example, if the source option is set to "http://example.com" and the user types foo, a GET request would be made to http://example.com?term=foo. The data itself can be in the same format as the local data described above.
因此,在您的PHP代码中,您必须这样做:
So in your PHP code you have to do:
include_once('../../dbConnect.inc.php');
$sql="SELECT name FROM artist WHERE `name` like '%".mysql_real_escape_string($_GET['term'])."%'";
$artist = select($sql);
$output_items = array();
while($row = mysql_fetch_array($artist)) {
$results[] = array('label' => $row['name']);
}
echo json_encode($results);
这篇关于自动完成显示所有条目,不执行任何搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!