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

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

问题描述

有人可以看看这个,让我知道我哪里出错了.我正在尝试让 jQuery UI 自动完成工作.这是我的代码:这是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('出了问题');$rows = array();而 ($row = mysql_fetch_assoc($result)){$rows[] = $row;}打印 json_encode($rows);?>

这是我的 javascript 内联脚本

这是自动"div

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

当我使用 firebug 查看调用时,我看到 search.php 正在返回

[{"Title":"罪恶之城"}]

jQuery 只是显示 UNDEFINED有什么想法吗??

解决方案

查看 jquery ui 自动完成文档.您返回的 JSON 与自动完成功能正在寻找的内容不匹配.您返回的对象必须具有名为 label 或 value(或两者)的属性.

您可以尝试以下选项:

选项 1:更改返回的 JSON

更改返回的 JSON 以包含标签/值属性,例如:

[{标签":罪恶之城"}]

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

[ 《罪恶之城》、《等等》]

选项 2:更改私有 _render 函数

更改自动完成的私有 _renderItem 函数以使用您的自定义属性,如此 自动完成示例所示(未经测试):

$( "#project" ).autocomplete({来源:./search.php",最小长度:3}).data(自动完成")._renderItem = function( ul, item ) {返回 $( "
  • ").data( "item.autocomplete", item ).append( item.Title ).appendTo(ul);};

    这有点灵活,但恕我直言更丑陋.

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

    this is my javascript inline script

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

    and this is the 'auto' div

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

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

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

    jQuery is just displaying UNDEFINED any ideas??

    解决方案

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

    You can try the following options:

    Option 1: Change returned JSON

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

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

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

    Option 2 : Change private _render function

    Change the private _renderItem function for the autocomplete to use your custom properties as shown in this autocomplete example (untested):

    $( "#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 自动完成 Mysql PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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