自动提示阿贾克斯PHP [英] auto suggest ajax for php
问题描述
我有一个HTML表单,PHP的纸条和jQuery。我需要一个AJAX code键使自动提示从我的PHP脚本。以下是code ...
i have an html form ,php scrip and jquery. i need a ajax code to make auto suggest from my php script. following is the code...
Form.html
<html>
<head>
<script src="jquery1.6.4.min.js" type="text/javascript"></script>
<script src="jquery.jSuggest.js" type="text/javascript"></script>
<link href="jSuggest.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" name="form1" method="post" action="#">
<input type="text" name="TagsInputField" id="TagsInputField"/>
</form>
</body>
</html>
的test.php
<?php
include("bc/script/core/dbcon.php");
$input = $_POST['TagsInputField'];
$data = array();
// query your DataBase here looking for a match to $input
$query = mysql_query("SELECT * FROM user WHERE username LIKE '%$input%'");
while ($row = mysql_fetch_assoc($query)) {
$json = array();
$json['value'] = $row['id'];
$json['name'] = $row['username'];
$data[] = $json;
}
header("Content-type: application/json");
echo json_encode($data);
?>
jquery.jSuggest.js
$(function() {
var dataSource = {
items: [
{
value: "21",
name: "Mick Jagger"},
{
value: "43",
name: "Johnny Storm"},
{
value: "46",
name: "Richard Hatch"},
{
value: "54",
name: "Kelly Slater"},
{
value: "79",
name: "Michael Jordan"}
]
};
$('#TagsInputField').jSuggest({
source: dataSource.items,
selectedItemProp: "name",
seekVal: "name",
selectionAdded: function(elem, data) {
console.log(data.name);
},
selectionRemoved: function(elem, data) {
console.log(data.name);
elem.remove();
}
});
});
注意指针源,它指的是一个对象dataSource.items阅读suggesions。 任何一个可以帮我写一个ajax code阅读suggesions FOM的PHP文件,该文件返回一个JSON。
notice the pointer "source" it is refering to an object "dataSource.items" to read suggesions. can any one help me to write an ajax code to read suggesions fom php file which is returning a json.
推荐答案
jSuggest使得在默认情况下的要求。你必须添加:
jSuggest makes GET requests by default. You have to add:
type: "POST"
在规则
有你的jSuggest规则,其他几个主要的错误。你应该阅读文档: http://scottreeddesign.com/project/jsuggest
There are a few other major errors in your jSuggest rules. You should read the documentation: http://scottreeddesign.com/project/jsuggest
这篇关于自动提示阿贾克斯PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!