我正在使用 jquery-ui-autocomplete 从我的 mysql 数据库中成功检索一个字段,但是我需要从列表中获取用户的选择,从数据库中重新选择并使用从数据库中检索到的地址填写多个表单.
这是我到目前为止工作的代码,但我不知道如何继续,以获得我需要的结果。场景是这样的 - 我有一个 INPUT 字段,其中填充了从数据库中检索到的邮政编码。然后用户选择一个邮政编码,从该选择中我需要数据库中的所有地址来填充单个表单。
我的 HTML 文件:
$(document).ready(function() {
$("#autocomplete").autocomplete({
autoFocus: true,
source: "search.php",
minLength: 2, //search after two characters
select: function(event, ui){
//do something
}
});
});
<body>
<input id="autocomplete" type="text" name="autocomplete" />
</body>
我的 search.php 文件:
// DB connection is done here
$term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends
$qstring = "SELECT DISTINCT zipcode FROM table WHERE zipcode LIKE '%".$term."%' ORDER BY zipcode ASC";
$result = mysql_query($qstring); //query the database for entries containing the term
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) //loop through the retrieved values
{
$row['zipcode'] = htmlentities(stripslashes($row['zipcode']));
$row['id'] = (int)$row['id'];
$row_set[] = $row['zipcode']; //build an array
}
echo json_encode($row_set); //format the array into json data
当然,我在查询中使用 DISTINCT 来消除多个相同的邮政编码。
谢谢您的帮助!罗伯特