JQuery UI自动完成搜索结果不显示
我使用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代码:
$(function() { //autocomplete $("#search").autocomplete({ source: "../searchclient.php", minLength: 1, }); });
输入字段:
我相信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" ],
然后自动完成function与该源一起使用。 所以数组传回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);
试试这个:
$("#search").autocomplete({ source: "../searchclient.php", minLength: 1, }).data("autocomplete")._renderItem = function(ul, item) { return $("").data("item.autocomplete", item).append("" + item.value + "").appendTo(ul); };
我走了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
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); ?>