我有我的索引页INDEX.html和一个PHP页面测试.php然后是一个自动建议的javascript代码。
我正在尝试制作一个自动建议搜索字段。
我没有收到自动建议输出。
仅显示NO RESULT FOUND
。
有人请帮我使自动建议显示在 http://code.drewwilson.com/entry/autosuggest-jquery-plugin 作品中。
<link href="css/autoSuggest.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="jquery.autoSuggest.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label for="q"></label> <input style="width: 300px;" type="text"
name="q" id="q" />
<script type="text/javascript">
$(document).ready(function(){
$("input[type=text]").autoSuggest("test.php",{minChars: 2, matchCase: false});});
</script>
</form>
</body>
</html>
PHP 看起来像这样:
<?php include("script/core/dbcon.php");
$input = $_REQUEST['q'];
$data = array();
// query your DataBase here looking for a match to $input
$query = mysql_query("SELECT * FROM user WHERE username LIKE '%$input%'");
while ($row = mysql_fetch_assoc($query)) {
$json = array();
$json['value'] = $row['id'];
$json['name'] = $row['username'];
$data[] = $json;
}
header("Content-type: application/json");
echo json_encode($data);
?>
jQuery现在有一个自动完成方法,它可以做同样的事情
你也应该更换
$("input[type=text]").auto----
跟
$("#q").auto-----