JQuery从数据库自动完成

JQuery Autocomplete from Database

本文关键字:数据库 JQuery      更新时间:2023-09-26

我需要为我的网站做自动完成建议,数据应该从数据库中检索。我想使用JQuery自动完成。这是我的代码,但它不起作用!这是我的php文件,名为gethint.hp:

<?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>

这花了很多时间,但我找不到问题。我在想是否有人能帮我。谢谢

<?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"]。

我做了一些更改,也许你需要修复一些东西,但看看是否有帮助。。。

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>

您的PHP脚本应该接受term参数,而不是q参数。