【问题标题】:JQuery auto complete can't fetch values from databaseJQuery自动完成无法从数据库中获取值
【发布时间】:2012-02-02 23:36:19
【问题描述】:

您好,我为使用 jquery 自动完成的输入文本编写代码,但 jquery 自动完成无法使用我的 php 代码从 mysql 获取数据。
我的代码有什么问题?
下面是我的javascript代码

<link href="_style/css/smoothness/jquery-ui-1.8.17.custom.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="_scripts/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="_scripts/js/jquery-ui-1.8.17.custom.min.js"></script>
<script type="text/javascript">
$(function() {
$( "#autocomplete" ).autocomplete({
source: "search.php",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
</script>
<body>
<input type="text" id="autocomplete" />
</body>

这是我的 php 代码:

<?php
include "Connect.php";
$term = trim(strip_tags($_GET['term']));
$qstring = "SELECT pName as value,pID FROM patient WHERE pName LIKE '%".$term."%'";
$result = mysql_query($qstring, $connection);//query the database for entries containing the term
while ($row = mysql_fetch_array($result))//loop through the retrieved values
{
$row['pName']=htmlentities(stripslashes($row['pName']));
$row['pID']=(int)$row['pID'];
$row_set[] = $row;//build an array
}
echo json_encode($row_set);//format the array into json data
?>

帮我解决我的代码问题!

【问题讨论】:

  • 你的php脚本返回数据了吗?
  • 我在没有自动完成编辑文本的情况下检查了我的 php 代码,并看到我的 php 代码工作正常!

标签: php jquery autocomplete


【解决方案1】:

使用 Firebug 拉起响应。它将为您的 JSON 结果制作一个表格,以便于调试。

【讨论】:

  • 如何在 firebug 中查看 JSON 结果?
  • 在 F12 菜单的控制台选项卡下。展开请求,然后单击 JSON。
  • 好的,感谢您的帮助,我可以看到 php 代码运行良好并返回数据。但是我看不到自动完成的结果!
  • 我可以在 JSON 中看到结果,但在自动完成建议中看不到它们!
【解决方案2】:

我不知道您的 jquery 函数是否有任何问题,但在这里我在您的 php 中看到的错误

取而代之的是:

   $row['pName']=htmlentities(stripslashes($row['pName']));

使用这个:

   $row['pName']=htmlentities(stripslashes($row['value']));

当您使用AS 时,您必须使用它来获取值

这是对您的 PHP 代码的完整更正

 $qstring = "SELECT pName as value,pID FROM patient WHERE pName LIKE '%".$term."%'";
 $result = mysql_query($qstring, $connection);

 $row_set = array(); //u have to define the row_set array 
 while ($row = mysql_fetch_array($result))
 {
     $row_set['pName']=htmlentities(stripslashes($row['value']));
     $row_set['pID']=(int)$row['pID'];
 }

 echo json_encode($row_set);

【讨论】:

  • 尝试更改数组的名称...您已经在使用row 进行获取
  • 查看我的更新答案,我测试了它并且它正在工作,否则你的 jquery 函数有问题
  • 感谢您的帮助,我可以在 firebug json 中看到数据,但自动完成不显示它们!
  • 如果您是 escaping 值,则自动完成在这种情况下将不起作用,因为 persion 单词将转换为 unicode 您必须解码 unicode
  • 如何使用波斯语自动完成?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
相关资源
最近更新 更多