2012-05-09 147 views
3

我是jQuery的新手。我在這裏想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"} 
    ] 

火蟲在搜索該文本框時顯示以下錯誤,並且沒有搜索建議。

如何解決此問題?

+0

顯然'$ _GET [「鳥」]'不存在。如果你用'$ _POST [「鳥」]替換它,會發生什麼? – Blazemonger

+0

$ q = strtolower($ _ POST [「birds」]);它導致相同的錯誤 –

+0

由於他調用'$ .getJSON()',它應該將數據加載到'$ _GET ['birds']'中。 PHP不會因爲沒有GET值而引發錯誤 - 它只會有一個空值。 –

回答

1

根據註釋,看起來第一個錯誤是由尋找錯誤的$ _GET變量引起的。道具你發現與Firebug和適當糾正。

目前未填充值的原因是JSON前附加了額外的字符串。刪除行echo $q;它應該工作。

+0

這太棒了....現在完美的作品。謝謝 –

相關問題