Remote jquery autocomplete data source

I am new to jQuery. I am trying to use jQuery autocomplete remote data source, this is my code:

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>

FILE: 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;

?>

get the following error:

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"}
    ] 

a fire error shows the following error when searching in this text box and there is no suggestion to search.

How to solve this problem?

+3
source share
1 answer

According to the comments, it seems that the first error was caused by finding the wrong variable $ _GET. Encourages you to determine what is with Firebug and fix it accordingly.

The reason it is not populating the value at the moment is because of the extra line echoed before JSON. Delete the line echo $q;and it should work.

+1
source

All Articles