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

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

问题描述

我需要为我的网站做自动完成建议和数据应当从数据库中检索。我想使用jQuery自动完成。这里是我的code,但它不工作!
这是我的PHP文件,gethint.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天全站免登陆