2014-08-28 92 views
1

我需要爲我的網站做自動完成建議,數據應該從數據庫中檢索。我想使用JQuery自動完成。這是我的代碼,但它不起作用! 這是我的PHP文件,gethint.php的名字:從數據庫JQuery自動完成

<?php 
require_once ('config.php'); 
$q=$_REQUEST["q"]; 
$sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'"; 
$result = mysql_query($sql); 
$json=array(); 

while($row = mysql_fetch_array($result)) { 
    $json[]=array(
    'value'=> $row['fname'], 
    'label'=> $row['fname'] 
    ); 
    } 
    echo json_encode($json); 
    ?> 

,然後這是我的HTML文件:

<html> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script> 
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" /> 
<script type="text/javascript"> 
$(document).ready(function(){     
$("#hint").autocomplete({       
source:'gethint.php', 
minLength:1     
    }); 
    });   
</script> 
</head> 
<body> 
<form class="sansserif" action="view.php" method="post"> 
Name: <input type="text" id="hint" name="hint" > 
<input type="submit" name="submit" value="View"> 
</form> 
</html> 

我花了很多時間,但我無法找到問題。我想知道是否有人可以幫助我。 謝謝。

+0

您是否試圖從$ json [] = array ...中刪除[] ...? 只是離開$ json = array(... – pecci 2014-08-28 16:13:17

+0

我試過這個以及bu再次不工作! – user2011036 2014-08-28 16:18:21

+0

btw感謝您的提示 – user2011036 2014-08-28 16:19:03

回答

1

我做了一些改變,也許你需要修復的東西,但看一看,看是否可以幫助...

的PHP:

<?php 
    require_once ('config.php'); 

    $q=$_REQUEST["q"]; 
    $sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'"; 
    $result = mysql_query($sql); 

    $json=array(); 

    while($row = mysql_fetch_array($result)) { 
     array_push($json, $row['fname']); 
    } 

    echo json_encode($json); 
?> 

的HTML + jQuery的:

<html> 
    <head> 
     <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
     <script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script> 
     <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" /> 
    </head> 
    <body> 
     <form class="sansserif" action="view.php" method="post"> 
      Name: <input type="text" id="hint" name="hint" /> 
      <input type="submit" name="submit" value="View"> 
     </form> 

     <script type="text/javascript"> 

     $(function() { 
      $("#hint").autocomplete({ 
       source: function(request, response) { 
        $.ajax({ 
         url: "gethint.php", 
         dataType: "jsonp", 
         data: { 
          q: request.term 
         }, 
         success: function(data) { 
          response(data); 
         } 
        }); 
       }, 
      }); 
     });  
     </script> 
    </body> 
</html> 
1

您的PHP腳本應該接受term參數,而不是q

+0

我改變了我的q參數爲term但不工作 – user2011036 2014-08-28 16:11:03

+0

你的腳本返回什麼? – vpzomtrrfrt 2014-08-28 21:26:48

2

你可以看到一個工作示例:http://zrift.com/jQueryAutocomplete/

<?php 
require_once ('..\config.php'); 
$q=$_REQUEST["term"]; 
$sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'"; 
$result = mysql_query($sql); 

while($row = mysql_fetch_array($result)) { 
    $json[]=array(
    'value'=> $row['fname'], 
    'label'=> $row['fname'] 
); 
} 
echo json_encode($json); 
?> 

我改變的只是$ _REQUEST [「q」]到$ _REQUEST [「term」]。