jQuery的自动完成PHP的Mysql [英] jQuery Autocomplete Mysql PHP

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

问题描述

喜会有人请看看这个,让我知道我要去哪里错了。我试图让jQuery用户界面自动完成工作。这是我的code:
这是search.php中

Hi could some one please take a look at this and let me know where I'm going wrong. I am trying to get jQuery UI autocomplete to work. this is my code: This is search.php

include "db_connect.php";
$search = $_GET['term'];    
    $result = mysql_query("SELECT Title FROM `movie` WHERE `Title` LIKE '%$search%' ORDER BY Title ASC") or die('Something went wrong');
    $rows = array();
    while ($row = mysql_fetch_assoc($result)){
        $rows[] = $row;

    }
print json_encode($rows);
?>

这是我的JavaScript嵌入脚本

this is my javascript inline script

<script type="text/javascript">
    $(document).ready(function()
    {
        $('#auto').autocomplete(
        {
            source: "./search.php",
            minLength: 3
        });
    });
</script>

这就是'自动'的div

and this is the 'auto' div

<div id="searchTxtFieldDiv">
<p><input type="text" id="auto" /></p>
</div>

当我使用Firebug查看来电我看到的search.php正在恢复

When I look at the call using firebug I see that search.php is returning

[{"Title":"Sin City"}]

jQuery是只显示未定义
任何想法??

jQuery is just displaying UNDEFINED any ideas??

推荐答案

看一看 jQuery用户界面自动完成文档。你正在返回的JSON不匹配自动完成所期待的。返回的对象必须有一个名为标签或值(或两者)的属性。

Have a look at jquery ui autocomplete documentation. The JSON you are returning does not match what the autocomplete is looking for. The object you return must have properties named label or value (or both).

您可以尝试以下选项:

改变JSON返回,包括标签/值属性,例如:

Change the JSON being returned to include the label/value properties such as:

[{"label":"Sin City"}]

从例子中它似乎也使用id属性。我相信上面的是自动完成,以显示值列表的最低要求。我想你也可以返回一个字符串数组,它会呈现在完全相同的方式为以上。

From the examples it also seems to use the id property. I believe the above is the minimum requirement for the autocomplete to display a list of values. I think you can also return an array of strings and it will render it in exactly the same way as the above.

[ "Sin City", "Etc" ]

选项2:更改私人_render功能

更改专用_renderItem功能自动完成使用您的自定义属性如本自动完成的例子(未经测试):

$( "#project" ).autocomplete({
    source: "./search.php",
    minLength: 3    
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
    return $( "<li></li>" )
   .data( "item.autocomplete", item )
   .append( item.Title )
   .appendTo( ul );
};

这是一个更灵活一点,但是更恶心恕我直言。

This is a bit more flexible but much uglier imho.

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

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