【问题标题】:JQuery Autocomplete from Database来自数据库的 JQuery 自动完成
【发布时间】:2014-10-22 13:31:17
【问题描述】:

我需要为我的网站提供自动完成建议,并且应该从数据库中检索数据。我想使用 JQuery 自动完成功能。这是我的代码,但它不起作用! 这是我的 php 文件,名称为 gethint.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)) {
  $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>

花了很多时间,但我找不到问题。我想知道是否有人可以帮助我。 谢谢。

【问题讨论】:

  • 您是否尝试从 $json[] = array... 中删除 []?只需离开 $json = array(...
  • 我也试过这个,但又不行!
  • 顺便感谢您的提示

标签: javascript php jquery jquery-ui autocomplete


【解决方案1】:
<?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"]。

【讨论】:

    【解决方案2】:

    我做了一些更改,也许您需要修复一些问题,但看看是否有帮助...

    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>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 你的脚本返回了什么?
      猜你喜欢
      • 2018-12-31
      • 2012-10-22
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      相关资源
      最近更新 更多