【问题标题】:jQuery UI Autocomplete with php and SQL not working使用 php 和 SQL 的 jQuery UI 自动完成功能不起作用
【发布时间】:2014-01-12 17:21:26
【问题描述】:

我是 php 的新手,我得到了 jQuery 的这段代码,但是有些事情发生了,我不知道, 我有表名“drug_info”,列“id,dru_name,dru_code,dru_unit,dru_price” 我想填充一个包含多个字段的表单,当我尝试编写从列表中选择的药物名称时,它会自动完成与药物信息相关的其他字段,这是代码。

这是 PHP。

<?php

include '../modules/config.php';
$drugs = array();
$query = "SELECT * FROM drug_info LIMIT 5 ";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$items[] = $row;
} 


$term = trim(strip_tags($_GET['term']));

$matches = array();
foreach($drugs as $drug){
if(stripos($drug['name'], $term) !== false){

    $drug['value'] = $drug['name'];
    $drug['label'] = "{$drug['dru_name']}, {$drug['dru_code']}  {$drug['dru_unit']}";
    $matches[] = $drug;
}
}

$matches = array_slice($matches, 0, 5);
print json_encode($matches);
?>

这是html。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>


<script type="text/javascript">

$(document).ready(function(){
var ac_config = {
    source: "xcore1.php",
    select: function(event, ui){
        $("#dru_name").val(ui.item.name);
        $("#dru_code").val(ui.item.code);
        $("#dru_unit").val(ui.item.unit
        );
    },
    minLength:1
};
$("#drug").autocomplete(ac_config);
});

</script>

<form action="#" method="post">
 <p><label for="name">Drug</label><br />
     <input type="text" name="name" id="name" value="" /></p>
 <p><label for="code">Code</label><br />
     <input type="text" name="code" id="code" value="" /></p>
 <p><label for="zip">Unit</label><br />
     <input type="text" name="unit" id="unit" value="" /></p>
</form>

我需要帮助,谢谢大家

【问题讨论】:

  • 我们知道有些东西不起作用。它在做什么呢?

标签: php jquery sql json


【解决方案1】:

一开始你在 $drugs 中有一个空数组。之后,您的代码从数据库中获取数据并将其放入变量 $items。

到目前为止还可以,但是您正在迭代仍然为空的 $drugs ?!你的代码应该做什么?

【讨论】:

  • 从数组中的数据库中获取值以供以后使用
  • 我认为 sanjeev 和我是对的。您是否已经尝试弄清楚您的浏览器从 AJAX 请求返回什么?
猜你喜欢
  • 2018-07-25
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
相关资源
最近更新 更多