我无法使用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
例如,它在数组中返回我想要的结果。
例如[{"value":"Hello World"},{"value":"East Meets West JV"}]
。
如果我替换了Jquery行
source: "../searchclient.php",
和
source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ],
然后自动完成功能将与该来源一起使用。因此,将数组传递回JQuery必须存在一个问题。
我不能完全把手指放在上面。我是否缺少一些重要的东西?
香港专业教育学院试图调试与萤火虫,但它不返回任何错误。
任何帮助将不胜感激!
编辑的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);
我沿着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);
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句