加载 json 数据以自动完成 jquery

发布于 2024-12-07 06:36:27 字数 1348 浏览 0 评论 0原文

我正在尝试使用 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&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2024-12-14 06:36:27

1. 您的 jQuery 代码不正确

您没有从远程域获取数据,因此您不需要 JSONP 请求。
应该是:

$("#actual_service").autocomplete({
    source: "http://dev_svr/medportal/search.php",
    minLength: 1
});

2. 您的 JSON 对象不正确。

自动完成的每个 json 对象应有两个值:labelvalue(无 ID)。如果您希望产品 id 为所选项目的值,产品名称为显示给用户的文本,则 json 对象应如下所示:

[{"value":50,"label":"ABDOMEN SUPINE&ERECT(2VIEWS)"},{"value":142,"label":"CT BRAIN"},{"value":115,"label":"CT CERVICAL SPINE"}]

编辑

您在评论中提到的内容,请尝试此 jQuery代码:

$('#actual_service').autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: 'http://dev_svr/medportal/search.php',
            dataType: 'jsonp',
            data: { term: request.term },
            success: function( data ) {
                    response( data );
            }
        });
    },
    minLength: 1
});

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:

$("#actual_service").autocomplete({
    source: "http://dev_svr/medportal/search.php",
    minLength: 1
});

2. Your JSON object is not correct.

Each json object for the autocomplete should have two values: label and value (no ID). 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:

[{"value":50,"label":"ABDOMEN SUPINE&ERECT(2VIEWS)"},{"value":142,"label":"CT BRAIN"},{"value":115,"label":"CT CERVICAL SPINE"}]

Edit

From what you mentioned in the comments, try this jQuery code:

$('#actual_service').autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: 'http://dev_svr/medportal/search.php',
            dataType: 'jsonp',
            data: { term: request.term },
            success: function( data ) {
                    response( data );
            }
        });
    },
    minLength: 1
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文