jQuery自动完成问题

时间:2012-07-11 17:36:47

标签: php jquery jquery-ui autocomplete

我是jQuery的新手,但是在阅读了很多关于自动完成的教程后,从数据库中提取了多个值,我想出了这个。自动填充字段不起作用。

包含自动填充字段的My Test.php页面位于:

 <?php
  define("_VALID_PHP", true);
  require_once("init.php");

?>
<?php include("header.php");?>
<meta charset="utf-8">








<style>
.ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.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 ).data( "autocomplete" ).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>

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

<div class="demo-description">
<p>Usage: Enter at least two characters to get bird name suggestions. Select a value to continue adding more names.</p>
<p>This is an example showing how to use the source-option along with some events to enable autocompleting multiple values into a single field.</p>
</div><!-- End demo-description -->

调用的search.php页面位于:

<?php
  define("_VALID_PHP", true);
  require_once("init.php");

//Connect to the Database
        require_once("lib/config.ini.php");
        $connect = mysql_connect($host,$username,$password);
        mysql_select_db($database, $connect);

$term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends

$qstring = "SELECT username as value FROM users WHERE username LIKE '%".$term."%'";
$result24 = mysql_query($qstring) ;//query the database for entries containing the term

while ($row24 = mysql_fetch_array($result24,MYSQL_ASSOC))//loop through the retrieved values
{
        $row24['value']=htmlentities(stripslashes($row24['value']));
        $row24['id']=(int)$row24['id'];
        $row_set[] = $row24;//build an array
}
echo json_encode($row_set);//format the array into json data

?>

0 个答案:

没有答案