2012-02-02 69 views
0

您好我编写的代码使用jQuery自动完成,但jQuery自动完成无法从MySQL使用我的PHP代码获取数据的输入文本。
我的代码有什么问题?下面
是我的javascript代码JQuery自动完成无法从数据库中获取值

<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 
?> 

帮助我解决我的代码问题!

+1

你的php脚本是否返回数据? – tftd 2012-02-02 23:41:25

+0

我检查我的php代码没有自动完成编辑文本,并看到我的PHP代码正常工作! – 2012-02-03 00:23:17

回答

0

我不知道是否有什么错在你的jQuery的功能,但在这里我看到的错误在你的PHP

而是这样的:

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

使用此:

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

当你使用AS比你必须使用它来获取值

H ERE是对您的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); 
+0

我做你的建议,但代码不起作用! – 2012-02-03 00:15:00

+0

尝试更改数组的名称...您已经在使用'row'取回 – 2012-02-03 00:17:52

+0

我更改了数组名称,但代码无效! – 2012-02-03 00:27:57

0

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

+0

如何在firebug中查看JSON结果? – 2012-02-03 00:34:13

+0

在F12菜单的控制台选项卡下。展开请求,然后单击JSON。 – 2012-02-03 00:35:40

+0

好的,谢谢你的帮助,我可以看到,PHP代码运行良好,并返回data.But我看不到结果与自动完成! – 2012-02-03 00:48:52