【发布时间】:2018-01-21 13:33:48
【问题描述】:
我有一个完美运行的带有 JQuery ui 自动完成功能的脚本。有一个显示用户名字和姓氏的搜索过程。但是在我的数据库中,还有用户的图片,我想在建议中显示它,并带有名字和姓氏。
(在数据库中,pic包含图片url)
脚本:
$(function() {
$("#search").autocomplete({
source: "autocomplete.php",
minLength: 1,
select: function(event, ui) {
var url = ui.item.id;
if(url != '') {
location.href = '...' + url;
}
},
html: true,
open: function(event, ui) {
$(".ui-autocomplete").css("z-index", 1000);
}
});
});
autocomplete.php
<?php
if ( !isset($_REQUEST['term']) ) {
exit;
}
$mysqli = new MySQLi($DB_host,$DB_login,$DB_pass,$DB_select);
$term = trim(strip_tags($_GET['term']));
$term = preg_replace('/\s+/', ' ', $term);
$a_json = array();
$a_json_row = array();
$a_json_invalid = array(array("id" => "#", "value" => $term, "label" => "Only letters and digits are permitted..."));
$json_invalid = json_encode($a_json_invalid);
if(preg_match("/[^\040\pL\pN_-]/u", $term)) {
print $json_invalid;
exit;
}
if ($data = $mysqli->query("SELECT * FROM users WHERE firstname LIKE '%$term%' OR lastname LIKE '%$term%' ORDER BY firstname , lastname")) {
while($row = mysqli_fetch_array($data)) {
$firstname = htmlentities(stripslashes($row['firstname']));
$lastname = htmlentities(stripslashes($row['lastname']));
$id= htmlentities(stripslashes($row['id']));
$pic= htmlentities(stripslashes($row['pic']));
$a_json_row["id"] = $id;
$a_json_row["value"] = $firstname.' '.$lastname;
$a_json_row["label"] = $firstname.' '.$lastname;
array_push($a_json, $a_json_row);
}
}
/* jQuery wants JSON data */
echo json_encode($a_json);
flush();
?>
请帮忙。
【问题讨论】:
标签: javascript php jquery jquery-ui autocomplete