Jquery UI自动完成空$ _REQUEST参数

时间:2014-03-31 15:49:10

标签: jquery jquery-ui autocomplete

我有一张表格

<div class='row' >
    <div class='large-12 columns search_bar'>
        <div class='row'>
            <form action='search.php'>
                <div class='large-8 large-offset-2 medium-8 columns search_home'><input type='text' id='search'></div>
            </form>
        </div>
    </div>
</div>

我从jQuery UI调用自动完成功能,如:

<script src="js/jquery-ui-1.10.4.custom.min.js"></script>
<script>    
    $(function() {
        $("search").autocomplete({ 
            source: "search_retailers.php",     // provide the name of the PHP source file here
            minLength: 2,
            select: function( event, ui ) {
            }
        })
    });
</script>

search.php的格式如下:

include ("dbinfo.php");
$search = $_REQUEST['search'];

$sql = mysql_query ("SELECT name FROM customers WHERE name LIKE '%".$search."%'");

while ($row=mysql_fetch_array($sql)) {
    $results[] = array('label' => $row['name']);    
}
echo json_encode ($results);

这个过程正在“工作”。因为我得到了JSON,但我得到了所有结果,经过进一步调查,$_REQUEST['search']是空白的,任何想法我都不知道了吗?

谢谢,

布雷特

1 个答案:

答案 0 :(得分:1)

我认为键入的数据term不是search,请尝试:

include ("dbinfo.php");
$search = $_REQUEST['term'];

$sql = mysql_query ("SELECT name FROM customers WHERE name LIKE '%".$search."%'");

while ($row=mysql_fetch_array($sql)) {
    $results[] = array('label' => $row['name']);    
}
echo json_encode ($results);

你错过了一个#来引用你#search的元素ID:

$(function() {
    $("#search").autocomplete({ 
        source: "search_retailers.php",     // provide the name of the PHP source file here
        minLength: 2,
        select: function( event, ui ) {
        }
    })
});