嗨,我发现这个插件到目前为止是最好的。和我想要的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文件放进去,但一直在说搜索时,它工作得很好……在我的文件中,我的数组结构有一些错误。
发布于 2011-07-18 13:09:18
主要问题是您传递的是嵌套数组
$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;https://stackoverflow.com/questions/6716557
复制相似问题