获取jQuery自动完成功能以使用PHP源代码
我有一个带有以下代码的jQuery自动填充字段:
I have a jQuery autocomplete field with this code:
var tags = ["a", "ab", "abc", "abcd", "adbce", "abcdef", "abcdefg", "abcdefgh", "abcdefghi", "abcdefghij", "abcdefghijk", "abcdefghijkl", "abcdefghijklm", "abcdefghijklmn", "abcdefghijklmno", "abcdefghijklmnop", "abcdefghijklmnopq", "abcdefghijklmnopqr", "abcdefghijklmnopqrs", "abcdefghijklmnopqrst", ];
$("input#name").autocomplete({
position: {
offset: "0 -10px",
},
source: tags
});
使用'tags'数组作为样本输入数据,它正常工作。
It worked correctly using the 'tags' array as sample input data.
现在我需要一组MySQL查询结果而不是那个样本数组。我所做的是将函数调用更改为:
Now I need to have a set of MySQL query results instead of that sample array. What I did was change the function call to this:
$("input#name").autocomplete({
position: {
offset: "0 -10px",
},
source: "http://absolutepathtofile/autosuggest.php"
});
我使用绝对路径确保我没有在那里犯一些愚蠢的错误,因为我可以不要让文件返回自动完成。我去过jQuery文档,发现了一些使用PHP / MySQL返回自动完成结果的例子,但我无法让它工作。
I used an absolute path to be sure I wasn't making some silly mistake there, because I can't get the file's return into the autocomplete. I've been to the jQuery documentation and found some examples of using PHP/MySQL to return results for the autocomplete but I can't get it to work.
这是我在autosuggest.php中尝试过的事情:
This is what I tried in autosuggest.php:
$term = $_REQUEST['term'];
$query = "SELECT * FROM merchants WHERE business_name LIKE '%$term%'";
$result = mysql_query($query);
$k=0;
while($row=mysql_fetch_array($result)){
$aUsers[$k]=$row['business_name'];
$k++;
}
echo json_encode($aUsers);
我尽可能简单但不起作用。
I made it as simple as possible but it didn't work.
然后我测试了JSON是否完全被发送所以我这样做了:
Then I tested to see if the JSON was being sent at all so I did this:
$array[0]="test";
$array[1]="test1";
echo json_encode($array);
它不起作用。我在任何地方都找不到这个问题,我做错了什么? PHP版本是5.3.10,它有json_encode(之前使用过)。
And it doesn't work. I can't find this problem anywhere, what am I doing wrong? PHP version is 5.3.10 and it has json_encode (used it before).
$.ajax({
url:"http://absolutepathtofile/autosuggest.php",
type:"post",
success:function(html){
$("#user_phone").autocomplete(
{position: {offset: "0 -10px"},
source: html
});
}
});
- 为我工作并进行测试