2012-11-15 32 views
0
的例子搜索

的index.php如何製作下拉列表?谷歌

<html> 
<head> 
    <title>ы</title> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> 
<script type="text/javascript" src="jquery.autocomplete.js"></script> 
</head> 
<body> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#search").keyup(function(){ 
      $("#search").autocomplete("json.php", { 
      delay:10, 
      minChars:4, 
      maxItemsToShow:3 
      } 
     ); 
      }); }); 
     //?word=" + $("#search-text").val() 
</script> 
<form action=""><input type="search" id="search"> <input type="submit"></form> 
<body> 
</html> 

json.php

<?php 
echo json_decode($_GET['q']); 

如何使此代碼工作?我如何使用json文件? 如何在PHP中處理請求並將結果返回到自動完成工作? 也許有更好的解決方案? 官方文檔不包含服務器端代碼示例!

看了迴應200ok。沒有人。 echo json_decode($ _GET ['q']);錯誤在這裏,問題在哪裏?

回答

0
$(function() { 
    $("#city").autocomplete({ 
     source: function(request, response) { 
      $.ajax({ 
       url: "http://sitetesting.kiev.ua/ajax.php", 
       dataType: "jsonp", 
       data: { 
        name_startsWith: request.term 
       }, 
       success: function(data) { 
        response($.map(data.geonames, function(item) { 
         return { 
          label: item.countryName 
         } 
        })); 
       } 
      }); 
     }, 
     minLength: 2 
    }); 
}); 
0

爲什麼你想使用PHP的自動完成?擊鍵後每使用js比使用js更重要嗎?

,無論如何你可以試試這個,而不是

<script> 
    $(function() { 
     var availableTags = [ 
      "ActionScript", 
      "AppleScript", 
      "Asp", 
      "BASIC", 
      "C", 
      "C++", 
      "Clojure", 
      "COBOL", 
      "ColdFusion", 
      "Erlang", 
      "Fortran", 
      "Groovy", 
      "Haskell", 
      "Java", 
      "JavaScript", 
      "Lisp", 
      "Perl", 
      "PHP", 
      "Python", 
      "Ruby", 
      "Scala", 
      "Scheme" 
     ]; 
     $("#tags").autocomplete({ 
      source: availableTags 
     }); 
    }); 
    </script> 

,並在身體

<div class="ui-widget"> 
    <label for="tags">Tags: </label> 
    <input id="tags" /> 
</div> 


</body> 
+0

我需要從數據庫/ – knowill

0

你可以使用這些方針的東西來填充你的項目數組,然後檢查是否值在現場匹配其中的任何一個。

$q = strtolower($_GET["q"]); 
if (!$q) return; 

/* Connect to database */ 

/* Query Database */ 

$items = array(); 

/* Loop through results and add them to items array */ 

while ($row = mysqli_fetch_assoc($result)) { 
    $items[$row['name']] = $row['value']; 
} 

foreach ($items as $key=>$value) { 
    if (strpos(strtolower($key), $q) !== false) { 
     echo "$key|$value\n"; 
    } 
} 

用於搜索的代碼(沒有數據庫查詢位)在http://jquery.bassistance.de/autocomplete/demo/

是記錄這當然是要生成陣列每次有人類型的信到輸入字段中的,因此是非常服務器密集。

上述更好的方法是使用availableTags變量並通過PHP將它們加載到加載的javascript變量中。我會建議使用PHP的json_encode函數。