自动建议 AJAX for PHP


auto suggest ajax for php

我有一个HTML表单,PHP Scrip和jQuery。 我需要一个 Ajax 代码来从我的 PHP 脚本进行自动建议。 以下是代码...

形式.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>  

测试

.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 的 suggesions fom php 文件。

jSuggest默认发出GET请求。您必须添加:

type: "POST"

在规则中。

jSuggest规则中还有其他一些主要错误。您应该阅读文档:http://scottreeddesign.com/project/jsuggest