您好我发现这个插件现在最好了。而我想要的 jQuery Plugin: Tokenizing Autocomplete Text Entry
但是当我试图制作我自己的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;
?>
我不希望它从数据库中获取数据,但是当我尝试在HTML页面上使用它时,只需使用数组或从xml文件中获取数据
<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文件但继续说搜索时它工作正常....用我的文件Defiantly我的ARRAY结构有一些错误。
答案 0 :(得分: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;