PHP,MYSQL自动完成不工作 [英] PHP, MYSQL Autocomplete does not working

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

问题描述

我要让自动完成文本框,从数据库中选择员工姓名。我没有关于公平的想法。但我试图使它如下。

I want to make auto complete text box for select employee names from the database. I don't have fair idea about that. But I tried to make it as following.

autocomplete.php

autocomplete.php

<?php
include 'func/db_connect.php';

if (isset($_POST['query'])) 
{
    $query = $_POST['query'];
    $mysql_query = mysql_query("SELECT * FROM employee WHERE name LIKE '%{$query}%'");

    while ($row = mysql_fetch_assoc($mysql_query)) 
    {
        $array[] = $row['name'];
    }
    echo  json_encode ($array);
}


js脚本


js script

<script>
    $('#typeahead').typeahead({
        source: function(typeahead, query){
            $.ajax({
                url: 'autocomplete.php',
                type: 'POST',
                data: 'query=' + query,
                dataType: 'JSON',
                async: 'false',
                success: function(data){
                    typeahead.process(data);
                }
            });
        }
    });
</script>


的index.php


index.php

<link rel="stylesheet" href="css/jquery-ui-1.10.3.custom.min.css" />
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery-ui-1.10.3.custom.min.js"></script>

                    <td><b>Employee name : </td>
                    <td>
                        <input type="text" id="typeahead" data-provide="typeahead" size="30">
                    </td>


但它不工作。什么是自动完成的化妆文本框的正确方法。


But it does not work. What is the correct way of make autocomplete text box.

推荐答案

尝试以下code ...

Try following code...

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/minified/jquery-ui.min.css" type="text/css" /> 

<p><label>Country:</label><input type='text' name='country' value='' class='auto'></p>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>


<script type="text/javascript">
$(function() 
{
    $(".auto").autocomplete({
        source: "autocomplete.php",
        minLength: 1
    });
});
</script>

autocomplete.php

<?php

mysql_connect("localhost", "root", "")or die("cannot connect"); 
mysql_select_db("test")or die("cannot select DB");

$query = $_GET['term'];

$mysql_query = mysql_query("SELECT * FROM users WHERE name LIKE '%{$query}%'");

while ($row = mysql_fetch_assoc($mysql_query))
{
    $array[] = $row['name'];
}
echo  json_encode ($array);

这篇关于PHP,MYSQL自动完成不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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