JQuery auto complete无法从数据库中获取值

时间:2012-02-02 23:36:19

标签: php jquery autocomplete

您好我为使用jquery auto complete的输入文本编写代码但是jquery auto complete无法使用我的php代码从mysql获取数据。
我的代码有什么问题?
下面是我的javascript代码

<link href="_style/css/smoothness/jquery-ui-1.8.17.custom.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="_scripts/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="_scripts/js/jquery-ui-1.8.17.custom.min.js"></script>
<script type="text/javascript">
$(function() {
$( "#autocomplete" ).autocomplete({
source: "search.php",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
</script>
<body>
<input type="text" id="autocomplete" />
</body>

这是我的PHP代码:

<?php
include "Connect.php";
$term = trim(strip_tags($_GET['term']));
$qstring = "SELECT pName as value,pID FROM patient WHERE pName LIKE '%".$term."%'";
$result = mysql_query($qstring, $connection);//query the database for entries containing the term
while ($row = mysql_fetch_array($result))//loop through the retrieved values
{
$row['pName']=htmlentities(stripslashes($row['pName']));
$row['pID']=(int)$row['pID'];
$row_set[] = $row;//build an array
}
echo json_encode($row_set);//format the array into json data
?>

帮我修复代码问题!

2 个答案:

答案 0 :(得分:0)

我不知道你的jquery函数是否有什么问题,但是我在你的php中看错了

取而代之的是:

   $row['pName']=htmlentities(stripslashes($row['pName']));

使用此:

   $row['pName']=htmlentities(stripslashes($row['value']));

当您使用AS时,必须使用它来获取值

这是对您的PHP代码的完整修正

 $qstring = "SELECT pName as value,pID FROM patient WHERE pName LIKE '%".$term."%'";
 $result = mysql_query($qstring, $connection);

 $row_set = array(); //u have to define the row_set array 
 while ($row = mysql_fetch_array($result))
 {
     $row_set['pName']=htmlentities(stripslashes($row['value']));
     $row_set['pID']=(int)$row['pID'];
 }

 echo json_encode($row_set);

答案 1 :(得分:0)

使用Firebug提取响应。它将创建一个JSON结果表,以便于调试。