dpjtn48868 2014-08-28 15:49
浏览 39
已采纳

来自数据库的JQuery自动完成

I need to to do autocomplete suggestion for my website and the data should be retrieved from database. I want to use JQuery autocomplete. here is my code but it doesn't work! This is my php file with the name of 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);
  ?>

and then this is my html file :

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

It took a lot of time but I couldn't find the problem. I was wondering if someone could help me. Thanks.

  • 写回答

3条回答 默认 最新

  • douhao5280 2014-08-28 16:23
    关注

    I did some changes, maybe you need to fix something but take a look to see if helps...

    The 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);
    ?>
    

    The 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿