2014-08-28 77 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”]。