2016-05-04 18:02 阅读 56

自动完成不起作用 - PHP - Mysql - jQuery

When I type anything in the text box it doesn't display anything. The search.php shows all the records if I hardcode $searchTerm='a';

Probably it is something in the jQuery I am missing.

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <link rel="stylesheet" href="//">
  <script src="//"></script>
  <script src="//"></script>
  $(function() {
    $( "#skills" ).autocomplete({
      source: 'search.php'

<div class="ui-widget">
  <label for="skills">skills: </label>
  <input id="skills">

MY PHP (search.php) page is:

 //get search term
        $searchTerm = $_GET['term'];

        //get matched data from skills table
        $query = $db->query("SELECT * FROM skills WHERE skill LIKE '%".$searchTerm."%' ORDER BY skill ASC");
        while ($row = $query->fetch_assoc()) {
            $data[] = $row['skill'];

        //return json data
        echo json_encode($data);


["A A EDU","A T Still","A.B.I. ","Aani", "Zane"]

Chrome Debug

enter image description here

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doucan8276 doucan8276 2016-05-04 19:40

    I can´t see the send params (data) to you server call.

    like this (but i haven´t right if this work):

    $(function() {
       $( "#skills" ).autocomplete({
          source: 'search.php',
          data: { 
             'term' : $('#skills').val()
          type: "GET"

    I prefer use a plugin to do this, in the method AJAX is possible set the results to list in field. like this:

        lookup: function (query, done) {
                url: "form.php",
                dataType: "json",
                data: { 
                        term     : query 
                type: "GET", //you can change to POST if u need
                success: function( response ){
                    var result = {
                        suggestions: response.results
                error: function(XMLHttpRequest, textStatus, errorThrown) { 
        onSelect: function (suggestion) {
            id_ofselected =; //set a var with selected object

    In the Php Side u have a code like this:

    $digits = $_GET['term'];
    $query = $db->query(SELECT name AS `value`, id AS `data` 
              FROM table 
              WHERE name LIKE '$digits%'");
    while ($row = $query->fetch_assoc()) {
        $data[] = $row['skill'];
        echo json_encode(array( 'results' => $cons ));
        echo json_encode(array( 'results' => array());  

    I use Autocomplete Jquery plugin. See [a link] (!

    点赞 评论 复制链接分享
  • dongyu4908 dongyu4908 2016-05-04 18:53
        source: function( request, response ) {
          $.getJSON( "search.php", {
            term: extractLast( request.term )
          }, response );
    点赞 评论 复制链接分享