简体   繁体   English

jQuery自动完成Mysql PHP

[英]jQuery Autocomplete Mysql 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. 我正在尝试使jQuery UI自动完成工作。 this is my code: This is search.php 这是我的代码:这是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 这是我的JavaScript内联脚本

<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 当我使用Firebug查看呼叫时,我看到search.php返回了

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

jQuery is just displaying UNDEFINED any ideas?? jQuery只是显示未定义的任何想法?

Have a look at jquery ui autocomplete documentation . 看看jquery ui自动完成文档 The JSON you are returning does not match what the autocomplete is looking for. 您返回的JSON与自动完成功能所寻找的不匹配。 The object you return must have properties named label or value (or both). 您返回的对象必须具有名为label或value(或两者)的属性。

You can try the following options: 您可以尝试以下选项:

Option 1: Change returned JSON 选项1:更改返回的JSON

Change the JSON being returned to include the label/value properties such as: 更改返回的JSON以包括标签/值属性,例如:

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

From the examples it also seems to use the id property. 从示例中,它似乎还使用了id属性。 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 选项2:更改private _render函数

Change the private _renderItem function for the autocomplete to use your custom properties as shown in this autocomplete example (untested): 更改私有_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. 这有点灵活,但恕我直言。

@Shaun Thanks mate, sometimes you just need someone to point out the obvious. @Shaun谢谢同伴,有时您只需要指出一个明显的地方即可。 The way I finally got it to work was 我最终使它工作的方式是

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

    $json = '[';
        $first = true;
        while ($row = mysql_fetch_assoc($result))
        {
            if (!$first) { $json .=  ','; } else { $first = false; }
            $json .= '{"value":"'.$row['Title'].'"}';
        }
        $json .= ']';
        echo $json;

atleast run mysql_real_escape_string() on $search before jamming it into a SQL Query. 至少在$ search上运行mysql_real_escape_string(),然后再将其阻塞到SQL查询中。

The source: bit in your javascript is probably messing it up. 来源:您的JavaScript中的代码可能将其弄乱了。 You should remove the . 您应该删除。 if 'search.php' is at the 'top' of the documentroot directory. 如果“ search.php”位于documentroot目录的“顶部”。

Firebug will probably help you take a peak at the communication as well. Firebug也可能会帮助您在交流方面达到顶峰。

I was going to say this, always sanitize user inputs. 我要说的是,总是清理用户输入。

It's hard to say since I never used jQuery autocomplete but I think you should target the value of "#auto". 很难说,因为我从未使用过jQuery自动完成功能,但我认为您应该将“ #auto”的值作为目标。 Something like this: $('#auto').val().autocomplet 像这样的东西:$('#auto')。val()。autocomplet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM