3

我是 jQuery 的新手。我正在尝试 jQuery 自动完成远程数据源,这是我的代码:

HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title></title>
    <meta name="GENERATOR" content="Quanta Plus">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
  </head>
  <body>
    <meta charset="utf-8">
    <style>
      .ui-autocomplete-loading {
        background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat;
      }
    </style>
    <script>
      $(function() {
        var cache = {},
            lastXhr;
        $( "#birds" ).autocomplete({
          minLength: 2,
          source: function( request, response ) {
            var term = request.term;

            if ( term in cache ) {
              response( cache[ term ] );
              return;
            }

            lastXhr = $.getJSON( "search.php", request, function( data, status, xhr ) {
              cache[ term ] = data;

              if ( xhr === lastXhr ) {
                response( data );
              }
            });
          }
        });
      });
    </script>
    <div class="demo">
      <div class="ui-widget">
        <label for="birds">Birds: </label>
        <input id="birds"  value="" />
      </div>
    </div><!-- End demo -->
  </body>
</html>

文件:search.php

<?php 

require_once("includes/connection.php");

$q = strtolower($_GET["birds"]);
echo $q;
$return = array();

$query = "SELECT title
  FROM `project`
  WHERE `title` LIKE CONVERT( _utf8 '%$q%'
  USING latin1 )";

$resultset=mysql_query($query,$connection);

$json = '[';
$first = true;

while ($row = mysql_fetch_assoc($resultset)) {
  if (!$first) {
    $json .=  ',';
  } else {
    $first = false;
  }

  $json .= '{"value":"'.$row['title'].'"}';
}

$json .= ']';

echo $json;

?>

收到以下错误:

Notice:
Undefined
  index: birds in /var/www/html/workbench/sudeepc/project/search.php
  on line 4
    [
      {"value":"chandrayaan"},
      {"value":"Project Management System"},
      {"value":"shoping cart"},
      {"value":"hospital management system"}
    ] 

火灾错误在该文本框上搜索时显示以下错误,并且没有搜索建议。

如何解决这个问题?

4

1 回答 1

2

根据评论,第一个错误似乎是由查找错误的 $_GET 变量引起的。支持你用 Firebug 发现它并适当地纠正它。

它当前没有填写值的原因是因为在 JSON 前面回显了额外的字符串。删除线echo $q;,它应该工作。

于 2012-05-09T18:48:27.630 回答