加载 json 数据以自动完成 jquery
我正在尝试使用 jqueryui 自动完成从我的表单上的 mysql 数据库加载服务,但没有任何反应?请帮助我或告诉我哪里错了 我的 html
<input type="text" id="actual_service" />
我的 javascript 脚本
$("#actual_service").autocomplete({
source: "http://dev_svr/medportal/search.php?callback=?",
dataType: "jsonp",
minLength: 1
});
这是 search.php
$con = mysql_connect('localhost', 'dev', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("medrep", $con);
$term = trim(strip_tags($_GET['term']));
$qstring = "select prod_id id,prod_name value FROM product where prod_type='SERVICE' and prod_name LIKE '%".$term."%' ORDER BY prod_name;";
$result = mysql_query($qstring);
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
$row['value']=htmlentities(stripslashes($row['value']));
$row['id']=(int)$row['id'];
$row_set[] = $row;
}
header("Content-type: application/json");
echo json_encode($row_set);
当我加载该页面时,当我输入任何内容时,该输入框上没有任何反应。
这是 http://dev_svr/medportal/search.php?term=ct 当我将 sql 限制为 3 行时
[{"id":50,"value":"ABDOMEN SUPINE&ERECT(2VIEWS)"},{"id":142,"value":"CT BRAIN"},{"id":115,"value":"CT CERVICAL SPINE"}]
I am trying to use the jqueryui autocomplete to load services from mysql database on my form but nothing happens?? please help me or tell me where am wrong
my html
<input type="text" id="actual_service" />
my javascript script
$("#actual_service").autocomplete({
source: "http://dev_svr/medportal/search.php?callback=?",
dataType: "jsonp",
minLength: 1
});
this is search.php
$con = mysql_connect('localhost', 'dev', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("medrep", $con);
$term = trim(strip_tags($_GET['term']));
$qstring = "select prod_id id,prod_name value FROM product where prod_type='SERVICE' and prod_name LIKE '%".$term."%' ORDER BY prod_name;";
$result = mysql_query($qstring);
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
$row['value']=htmlentities(stripslashes($row['value']));
$row['id']=(int)$row['id'];
$row_set[] = $row;
}
header("Content-type: application/json");
echo json_encode($row_set);
when i load that page nothing happens on that inputbox when i type anything.
this is a sample output of http://dev_svr/medportal/search.php?term=ct when i limit the sql to 3 rows
[{"id":50,"value":"ABDOMEN SUPINE&ERECT(2VIEWS)"},{"id":142,"value":"CT BRAIN"},{"id":115,"value":"CT CERVICAL SPINE"}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1. 您的 jQuery 代码不正确
您没有从远程域获取数据,因此您不需要
JSONP
请求。应该是:
2. 您的 JSON 对象不正确。
自动完成的每个 json 对象应有两个值:
label
和value
(无ID
)。如果您希望产品 id 为所选项目的值,产品名称为显示给用户的文本,则 json 对象应如下所示:编辑
您在评论中提到的内容,请尝试此 jQuery代码:
1. Your jQuery code is not correct
You are not getting data from a remote domain, therefore you don't need a
JSONP
request.It should be:
2. Your JSON object is not correct.
Each json object for the autocomplete should have two values:
label
andvalue
(noID
). If you want the product id to be the value of the selected item, and the product name to be the text that is shown to the user, then the json object should be like:Edit
From what you mentioned in the comments, try this jQuery code: