2011-09-14 82 views
-1

我的PHP代碼有問題看起來像使用jQuery UI自動完成

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代碼如下

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

和HTML標記

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

有什麼不好的代碼?它不會生成自動完成選項..在PHP日誌文件沒有錯誤。如何解決這個問題?

回答

0

假設JSON是好的,那麼你是不是做與數據的任何你回來

select: function(event,ui){ 
// do something with the data you get back 
//you would usually have a function here to do something with data 
// but this sample should be enough for you 
var id = ui.id; // id you got back assign to var 
var label = ui.label; // label you got back assign to var 
$('#gothisback').val(ui.id); //push it to a div 
    } 
+0

其格?我想把它作爲自動完成菜單取回 –