简体   繁体   English

jQuery UI自动完成与数据库

[英]Jquery-ui autocomplete with DB

Please suggest good tutorial to feed jquery ui qutomplete with php and mysql or help me to fix the problem of my code. 请建议一个很好的教程,用php和mysql来提供jquery ui qutomplete或帮助我解决我的代码问题。 Tried all methods. 尝试了所有方法。 Can't find wrong piece of code. 找不到错误的代码段。

My php code looks like that 我的PHP代码看起来像这样

if (isset($_REQUEST['term']))
{
    $term = trim(strip_tags($_REQUEST['term']));//retrieve the search term that autocomplete sends

    $result = $db->query("SELECT company as value,id FROM main WHERE company LIKE '$term'") or die(mysqli_error());;
    $results = array();
    while ($row = $result->fetch_row()) $results[] = array( 'id' => $row[0] , 'label' => $row[1], 'value' => $row[1] );
    echo json_encode($results);
}

Js code below 下面的js代码

$("#auto").autocomplete({
    source: "index.php",
    minLength: 2,//search after two characters
    select: function(event,ui){
    }
});

And HTML markup 和HTML标记

<input id="auto" name="company"/>

What's wrong with the code? 代码有什么问题? It doesn't generate autocomplete option.. No error in php log file. 它不会生成自动完成选项。.php日志文件中没有错误。 How to fix that problem? 如何解决这个问题?

Source param must be of type string, array or callback, and " Defines the data to use ", not the page used to get them. 源参数必须是字符串,数组或回调类型,并且“ 定义要使用的数据 ”,而不是用于获取它们的页面。 You should use something like (see here for an example ): 您应该使用类似的东西(请参阅此处的示例 ):

    $("yourSelector").autocomplete({  
        source: function(req, add){  
            $.get("index.php?...", function(data) {  
                var returnData= [];  
                $.each(data, function(i, val){  
                  returnData.push(val.property);  
                });  
                add(returnData);  
            });  
        }, [...]

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

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