【发布时间】:2011-07-16 09:35:56
【问题描述】:
嗨,我发现这个插件是迄今为止最好的。和我想要的 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 文件时它工作正常,但一直说 Searching.... 对于我的文件,我的 ARRAY 结构有一些错误。
【问题讨论】:
标签: php json jquery jquery-plugins