我没有使用php从我的mysql数据库中获取搜索结果。
PHP代码:
require_once "connectmysql.php";
$belongsto=$current_user->businessname;
$q = trim(strip_tags($_GET["term"]));
if (!$q) return;
$sql = "select clientname as value from zb_clients where clientname LIKE '%".$q."%' AND belongsto='".$belongsto."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$row['value']=htmlentities(stripslashes($row['value']));
$row_set[] = $row;
}
echo json_encode($row_set);
JQuery代码:
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/minified/jquery-ui.min.css" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
//autocomplete
$("#search").autocomplete({
source: "../searchclient.php",
minLength: 1,
});
});
</script>
输入字段:
<input type="text" name="search" id="search" placeholder="Search for Business Name" />
我相信php代码是正确的。如果我自己运行php代码并使用
/searchclient.php?term=a
作为示例,它返回我想要的数组结果。
e.g。 [{"value":"Hello World"},{"value":"East Meets West JV"}]
。
如果我替换Jquery行
source: "../searchclient.php",
与
source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ],
然后自动完成功能与该源一起使用。所以数组传回JQuery一定存在问题。
我无法完全按手指。我错过了一些关键的东西吗
我尝试用firebug进行调试,但它没有返回任何错误。
非常感谢任何帮助!
编辑的PHP代码:
require_once "connectmysql.php";
$belongsto=$current_user->businessname;
$q = $_GET["term"];
if (!$q) return;
$sql = "select clientname as value, idzb_clients as id from zb_clients where clientname LIKE '%".$q."%' AND belongsto='".$belongsto."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$row['id']=htmlentities(stripslashes($row['id']));
$row['value']=htmlentities(stripslashes($row['value']));
$row['label']=htmlentities(stripslashes($row['value']));
$row_set[] = $row;
}
echo json_encode($row_set);
答案 0 :(得分:0)
试试这个:
$("#search").autocomplete({
source: "../searchclient.php",
minLength: 1,
}).data("autocomplete")._renderItem = function(ul, item) {
return $("<li>").data("item.autocomplete", item).append("<a>" + item.value + "</a>").appendTo(ul);
};
答案 1 :(得分:0)
我走了AJAX路线,这似乎有效:
<强> HTML 强>
$( "#search" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "../searchclient.php",
dataType: "json",
data: {
q: request.term
},
success: function( data ) {
response( data );
}
});
},
minLength: 1
});
<强> SEARCHCLIENT.PHP 强>
<?php
require_once "connectmysql.php";
$belongsto = $current_user->businessname;
$q = $_GET['q'];
$sql = "select clientname as value, idzb_clients as id, has_ledger_setup from zb_clients where clientname LIKE '%".$q."%' AND belongsto='".$belongsto."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$row['id'] = htmlentities(stripslashes($row['id']));
$row['value'] = htmlentities(stripslashes($row['value']));
$row['label'] = htmlentities(stripslashes($row['value']));
$row_set[] = $row;
}
header('Content-Type: application/json');
echo json_encode($row_set);
?>