0

I want to limit search to Names using jqueryUi's auto-complete so that results returned are only Names and don't include other array values like fax and stuff.

Here's my php.

<?php
require_once 'db_conx.php';
$req = "SELECT * "
    ."FROM profiles "
    ."WHERE name LIKE '%".mysql_real_escape_string($_REQUEST['term']) ."%' "; 
$query = mysql_query($req);
while($row = mysql_fetch_array($query))
{
    $return = array ('label' => $row['name'],
    'founder' => $row['founder'],
    'fax' => $row['fax']

    );

}
echo json_encode($return);
?>

Javascript

$(function() {
        $( "#SearchInput").autocomplete({
            source: '../Search/BP_Search.php',
            minLength: 2,
            autoFocus: false,
            select: function(event, ui) {
             $('#ProName' ).html(ui.item.name); 
             $('#ProFax' ).html(ui.item.fax);  
             $('#ProFounder' ).html(ui.item.founder); 
            }
    });

Thanks.

4

1 回答 1

0

source是对象数组 -[ { label: "Choice1", value: "value1" }, ... ]时,仅在建议菜单http://api.jqueryui.com/autocomplete/#option-sourcelabel中搜索/显示(或者value如果label未提供)。因此,您可以在返回的数组中包含、和其他键/值集,并且它们不会被搜索/显示。 faxfounder

另一种方法是您仅在 php 脚本中获取名称,然后select执行 ajax 请求以获取所有其他数据。


注意:您正在使用ui.item.name-

$('#ProName' ).html(ui.item.name);

name不在你的数组中,所以应该是label

$('#ProName' ).html(ui.item.label);
于 2013-10-31T14:47:16.710 回答