【问题标题】:Jquery ajax autocomplete pluginjQuery ajax 自动完成插件
【发布时间】: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


    【解决方案1】:

    主要问题是你传递嵌套数组

       $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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-13
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 2013-06-02
      • 2012-01-29
      • 1970-01-01
      相关资源
      最近更新 更多