使用php和mysql进行Jquery“自动完成”

时间:2015-01-09 16:00:47

标签: php jquery

我按照此链接使用jquery,php和mysql构建多值自动完成系统。例如:如果我在输入字段中键入内容,它将在输入框中显示搜索结果,如果搜索其他字词,它将再次向我显示另一个结果,该结果将以逗号分隔显示结果。像这样的链接:http://jqueryui.com/autocomplete/#multiple-remote。代码如下:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Multiple, remote</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
.ui-autocomplete-loading {
  background: white url("loading-image.gif") right center no-repeat;
}
</style>
<script>
$(function() {
  function split( val ) {
    return val.split( /,\s*/ );
  }
  function extractLast( term ) {
    return split( term ).pop();
  }

  $( "#birds" )
    // don't navigate away from the field on tab when selecting an item
    .bind( "keydown", function( event ) {
      if ( event.keyCode === $.ui.keyCode.TAB &&
          $( this ).autocomplete( "instance" ).menu.active ) {
        event.preventDefault();
      }
    })
    .autocomplete({
      source: function( request, response ) {
        $.getJSON( "search.php", {
          term: extractLast( request.term )
        }, response );
      },
      search: function() {
        // custom minLength
        var term = extractLast( this.value );
        if ( term.length < 2 ) {
          return false;
        }
      },
      focus: function() {
        // prevent value inserted on focus
        return false;
      },
      select: function( event, ui ) {
        var terms = split( this.value );
        // remove the current input
        terms.pop();
        // add the selected item
        terms.push( ui.item.value );
        // add placeholder to get the comma-and-space at the end
        terms.push( "" );
        this.value = terms.join( ", " );
        return false;
      }
    });
});
</script>
</head>
<body>   

<div class="ui-widget">
  <label for="birds">Birds: </label>
  <input id="birds" size="50">
</div>

</body>
</html>

Php cdoe(它还没有完全完整,因为我不明白我需要在这个php页面做什么。)

<?php
require_once("../frontend/config.php");
$term = $_GET['term'];
$sql = mysqli_query($link, "SELECT family_name FROM contact_details WHERE family_name LIKE '%$term%' ");
while($res = mysqli_fetch_array($sql)){
    $family_name = $res['family_name'];
    echo json_encode($family_name);
}
?>

有什么能帮到我的,我怎么能像链接一样显示搜索结果?注意:我正在学习Jquery :)谢谢。

1 个答案:

答案 0 :(得分:1)

我这样做了。 Php代码应该是这样的:

<?php
require_once("../frontend/config.php");
$term = $_GET['term'];
$sql = mysqli_query($link, "SELECT family_name FROM contact_details WHERE family_name LIKE '%$term%' ");
 $return_arr = array();
 while ($row = mysqli_fetch_array($sql, MYSQL_ASSOC)) {
    $row_array['label'] = $row['family_name'];
    //$row_array['value'] = $row['tags_list'];
    //$row_array['abbrev'] = $row['abbrev'];

    array_push($return_arr, $row_array);
}

echo json_encode($return_arr);
?>