2011-07-16 196 views
0

嗨,我發現這個插件是最好的現在。並且我想 jQuery Plugin: Tokenizing Autocomplete Text EntryJquery ajax自動完成插件

但是,當我試圖讓自己的PHP文件,它不工作

我的PHP文件:

<?php 
$arr= array(
array("id"=>1,"name"=>"Ruby"), 
array("id"=>1,"name"=>"Kritya") 
); 
var_dump($arr); 
$json_response = json_encode($arr); 
$json_response = $_GET["callback"] . "(" . $json_response . ")"; 
echo $json_response; 
?> 

他們給了我這有一個sample.php文件這樣的:

<? 

# 
# Example PHP server-side script for generating 
# responses suitable for use with jquery-tokeninput 
# 

# Connect to the database 
mysql_pconnect("host", "username", "password") or die("Could not connect"); 
mysql_select_db("database") or die("Could not select database"); 

# Perform the query 
$query = sprintf("SELECT id, name from mytable WHERE name LIKE '%%%s%%' ORDER BY popularity DESC LIMIT 10", mysql_real_escape_string($_GET["q"])); 
$arr = array(); 
$rs = mysql_query($query); 

# Collect the results 
while($obj = mysql_fetch_object($rs)) { 
    $arr[] = $obj; 
} 

# JSON-encode the response 
$json_response = json_encode($arr); 

# Optionally: Wrap the response in a callback function for JSONP cross-domain support 
if($_GET["callback"]) { 
    $json_response = $_GET["callback"] . "(" . $json_response . ")"; 
} 

# Return the response 
echo $json_response; 

?> 

我不希望它從數據庫中獲取數據,但只使用一個陣列或者從XML文件中的數據,當我嘗試使用的HTML PAG Ë

<h2 id="theme">Facebook Theme</h2> 
<div> 
    <input type="text" id="demo-input-facebook-theme" name="blah2" /> 
    <input type="button" value="Submit" /> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     $("#demo-input-facebook-theme").tokenInput("/test.php", { 
      theme: "facebook" 
     }); 
    }); 
    </script> 
</div> 

它工作正常,當我把他們的網站的PHP文件,但口口聲聲說搜索....隨着我的文件挑釁有一些錯誤與我的陣列結構。

回答

0

的主要問題是要傳遞坐落陣列

$arr= array(
    array("id"=>1,"name"=>"Ruby"), 
    array("id"=>1,"name"=>"Kritya") 
    ); 

當插件預計它包裝了一堆從mysql_fetch_object命令對象的數組。

也許這可能工作:

$arr=array(); 
    $object = new stdClass(); 
    $object->id = 1; 
    $object->name = "Ruby"; 
    $arr[]=$object; 
    $object = new stdClass(); 
    $object->id = 1; 
    $object->name = "Kritya"; 
    $arr[]=$object; 
+0

都能跟得上犯規作品 – kritya