来自数据库的 JQuery 自动完成 [英] JQuery Autocomplete from Database

查看:22
本文介绍了来自数据库的 JQuery 自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

然后这是我的 html 文件:

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.

推荐答案

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

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

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:

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>

这篇关于来自数据库的 JQuery 自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆