2012-02-02 115 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