【发布时间】:2014-10-22 13:31:17
【问题描述】:
我需要为我的网站提供自动完成建议,并且应该从数据库中检索数据。我想使用 JQuery 自动完成功能。这是我的代码,但它不起作用! 这是我的 php 文件,名称为 gethint.php:
<?php
require_once ('config.php');
$q=$_REQUEST["q"];
$sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'";
$result = mysql_query($sql);
$json=array();
while($row = mysql_fetch_array($result)) {
$json[]=array(
'value'=> $row['fname'],
'label'=> $row['fname']
);
}
echo json_encode($json);
?>
然后这是我的 html 文件:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
<script type="text/javascript">
$(document).ready(function(){
$("#hint").autocomplete({
source:'gethint.php',
minLength:1
});
});
</script>
</head>
<body>
<form class="sansserif" action="view.php" method="post">
Name: <input type="text" id="hint" name="hint" >
<input type="submit" name="submit" value="View">
</form>
</html>
花了很多时间,但我找不到问题。我想知道是否有人可以帮助我。 谢谢。
【问题讨论】:
-
您是否尝试从 $json[] = array... 中删除 []?只需离开 $json = array(...
-
我也试过这个,但又不行!
-
顺便感谢您的提示
标签: javascript php jquery jquery-ui autocomplete