Php jQuery自动完成数据
目前正在实现jQuery autocomplete,数据从php文件传回并填充字段,但autocomplete下拉列表中没有任何文本。您可以选择条目 图片: 好像结果的文本没有被传回Php jQuery自动完成数据,php,jquery,Php,Jquery,目前正在实现jQuery autocomplete,数据从php文件传回并填充字段,但autocomplete下拉列表中没有任何文本。您可以选择条目 图片: 好像结果的文本没有被传回 try { $stmt = $db->prepare('SELECT id, firstname, surname, department, email FROM users WHERE firstname LIKE :term'); $stmt->execute(array('term
try {
$stmt = $db->prepare('SELECT id, firstname, surname, department, email FROM users WHERE firstname LIKE :term');
$stmt->execute(array('term' => '%'.$_GET['term'].'%'));
while($row = $stmt->fetch()) {
$row_array['id'] = $row['id'];
$row_array['firstname'] = $row['firstname'];
$row_array['surname'] = $row['surname'];
$row_array['email'] = $row['email'];
$row_array['department'] = $row['department'];
$return_arr[] = $row_array;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
/* Toss back results as json encoded array. */
echo json_encode($return_arr);
}
//jQuery
$(document).ready(function() {
$(".auto").autocomplete({
source: "search.php",
minLength: 1,
datatype: 'json',
select: function( event, ui ) {
$('#firstname').val(ui.item.firstname)
$('#surname').val(ui.item.surname);
$('#department').val(ui.item.department);
$('#email').val(ui.item.email);
$('#hidden').val(ui.item.id);
}
});
});
正如文档中所述,在最简单的形式中,JSON响应可以是和字符串数组:
[ "Choice1", "Choice2" ]
它也可以是包含标签和值的对象数组,以显示在自动完成字段中,例如:
[ { label: "Choice1", value: "value1" }, ... ]
实际上,jQueryUIAutoComplete插件无法处理返回的数据结构
请参见此处的详细文档:
希望这有帮助。如文档中所述,以最简单的形式,您的JSON响应可以是和字符串数组:
[ "Choice1", "Choice2" ]
它也可以是包含标签和值的对象数组,以显示在自动完成字段中,例如:
[ { label: "Choice1", value: "value1" }, ... ]
实际上,jQueryUIAutoComplete插件无法处理返回的数据结构
请参见此处的详细文档:
希望这有帮助