我正在尝试链接到下拉菜单,换句话说,当用户在输入字段中键入内容时,他会得到建议,但该建议必须是链接。我尝试使用json_encode,在这个变种中我只得到文本,我也尝试从php生成链接,但在这种情况下得到错误。有人帮忙吗?感谢。
HTML代码
<input id="handleSearchBtn" type="text" class="form-control ui-widget">
使用PHP的jQuery代码
$( function() {
var availableTags = [
<?php
$db = mysqli_connect("localhost", "root", "", "database");
$sql = "SELECT CRMContact FROM tb_users WHERE id = " . $_REQUEST["viewId"];
$query = mysqli_query($db, $sql);
if (mysqli_num_rows($query) == 1) {
$row = mysqli_fetch_object($query);
$crm = "<a href='view_user.php'>" . $row->CRMContact . "</a>";
} else {
$crm = "";
}
//this will output only text
// when remove json_encode i got error
// SyntaxError: expected expression, got '<'
echo json_encode($crm);
?>
];
$( "#handleSearchBtn" ).autocomplete({
source: availableTags
});
});