2012-02-15 33 views
0

我想使用jQuery的自動完成「功能」與PHP文件作爲源。我不明白爲什麼它不起作用。如果我使用在變量中輸入的數據,一切正常。希望有人能幫忙。預先感謝您的回覆。乾杯。馬克。jQuery的自動完成與PHP源代碼

我的HTML:

<input id="moi" type="text"/> 

我的JS:

$(function() { 
     $("#moi").autocomplete({ 
      source: "php/search_loc.php", 
      minLength: 2 
     }); 
    }); 

我的PHP:

<?php 
header('Content-Type: text/html; charset=utf-8'); 
require("../inc/connect.inc.php"); 
mysql_set_charset('utf8'); 

$result = mysql_query("SELECT * FROM search_loc"); 
$row=mysql_fetch_assoc($result); 

while($row=mysql_fetch_assoc($result)){ 
    echo $row['srl_loc'].'<br>';} 

?> 
+2

我從來沒有使用jQuery的自動完成插件,但我相當肯定,它不是由''
界定。 – Ryan 2012-02-15 16:11:30

+0

並且您有兩次'$ row = mysql_fetch_assoc($ result);'在那裏。這是故意的嗎? – 2012-02-15 16:39:53

回答

3

你的PHP腳本應該返回JSON數據,

在jQuery UI的autocomplete doc

The datasource is a server-side script which returns JSON data, 
+1

你可以在?之後放置<[?php,「]」之前的「[」],並用「,」分隔你的字符串。或者在你放置你的值的數組上使用json_encode – 2012-02-15 16:14:42

0

把它包在一個json_encode()功能:

echo json_encode($row['srl_loc']).'<br />';