当我尝试使用下面的代码实现自动完成时,我什么都没得到(没有结果,没有错误):
$('#keyword').autocomplete({
source : '/Dev/pages/search.php',
minLength : 3,
type : 'POST',
select: function( event, ui )
{
$(this).data("autocomplete").menu.element.addClass("yellow");
}
})
.data( "ui-autocomplete" )._renderItem = function( ul, item )
{
console.log(item);
return $( "<li>" )
.append( "<a>" + add3Dots(item.name,20) + "</a>" )
.appendTo( ul );
};
if(isset($_POST['term'])) {
//Word enter by user
$q = htmlentities($_POST['term']);
$search = connection::bdd_test();
$query = "SELECT name from BDD_TEST.companies WHERE name LIKE '%".$q."%' ORDER BY name asc";
$result = $search->query($query);
while($data = $result->fetch(PDO::FETCH_ASSOC)) {
$data['name'];
}
}
else
{
$data['call']=false;
$data['message']="Problem to collect word";
}
echo json_encode($data);
谁能告诉我出了什么问题?
我认为它可能是数据(),但我不确定。
答案 0 :(得分:0)
PHP - search.php
$a_json = array();
$a_json_row = array();
$search = connection::bdd_test();
$query = "SELECT name from BDD_TEST.companies ORDER BY name asc";
$result = $search->query($query);
while($data = $result->fetch(PDO::FETCH_ASSOC)) {
$a_json_row["name"] = $data['name'];
array_push($a_json, $a_json_row);
}
$json = json_encode($a_json);
print_r($json);
jQuery-ui自动填充
$( "#keyword" ).autocomplete({
source: "/Dev/pages/search.php",
minLength: 3,
select: function( event, ui ) {
$(this).data("autocomplete").menu.element.addClass("yellow");
}
});
确定一下:
我认为如果一切都很好,那么代码就可以了。