0
我有一個html表單,php scrip和jquery。我需要一個Ajax代碼來從我的PHP腳本中進行自動建議。以下是代碼...自動爲php提示ajax
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」來讀取提示。 任何人都可以幫助我編寫ajax代碼來讀取正在返回json的PHP文件的提示。
我需要一個AJAX代碼替換'VAR數據源= {項目:[ {值: 「21」,名稱: 「米克·賈格爾」}, {值: 「43」,名稱: 「約翰尼風暴」}, {值: 「46」,名稱: 「理查德艙口」}, {值: 「54」,名稱: 「凱利斯萊特」}, {值: 「79」,名稱:「 Michael Jordan「} ] };這段代碼tushar – nikki