为什么这个jQuery自动完成功能不起作用?

时间:2011-09-21 05:55:12

标签: javascript jquery jquery-plugins

我的页面标题中有以下内容

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="javascript/autoSuggest.js"></script>
<script type="text/javascript" src="javascript/suggest.js"></script>

suggest.js由:

组成
$(function(){

$("#idName input").autoSuggest("../Test.php", {minChars: 2, matchCase: true});
});

和autoSuggest.js是Drew Wilson的插件(http://code.drewwilson.com/entry/autosuggest-jquery-plugin

Test.php是

<?php
include('database_info.inc');
$input = $_POST["idName"];
$data = array();
var_dump($data);
// query database to see which entries match the input
$query = mysql_query("SELECT * FROM test WHERE title LIKE '%$input%'");
while ($row = mysql_fetch_assoc($query)) {
$json = array();
$json['value'] = $row['id'];
$json['name'] = $row['title'];
$data[] = $json;
}
header("Content-type: application/json");
echo json_encode($data);

?>

我的var_dump()没有做任何事情,也没有建议任何项目......我可能做错了什么?好像没有与Test.php的沟通

1 个答案:

答案 0 :(得分:0)

快速浏览一下插件表明它希望一个名为q的参数作为GET字符串传递,而不是作为POST。

<?
    $input = $_GET["q"];
    $data = array();
    // query your DataBase here looking for a match to $input
    $query = mysql_query("SELECT * FROM my_table WHERE my_field LIKE '%$input%'");
    while ($row = mysql_fetch_assoc($query)) {
    $json = array();
    $json['value'] = $row['id'];
    $json['name'] = $row['username'];
    $json['image'] = $row['user_photo'];
    $data[] = $json;
    }
    header("Content-type: application/json");
    echo json_encode($data);
?>