jQuery自动完成显示空结果

时间:2017-07-06 05:31:31

标签: php jquery jquery-ui jquery-ui-autocomplete

我有这个PHP脚本来从我的数据库中获取所有患者姓名:

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once('connection.php');
header("Content-type:application/json");
$cid = $_SESSION['clinic_id'];

$res = array();
$getPatients = "SELECT * FROM patient WHERE clinic_id = :cid  ORDER BY patient_id DESC";

$execGetPatients = $conn->prepare($getPatients);
$execGetPatients->bindValue(':cid', $cid);
$execGetPatients->execute();
$getPatientsResult = $execGetPatients->fetchAll();

$i = 0;
foreach($getPatientsResult as $result)
{
    $res[$i] = $result;
    $i++;
}

echo json_encode($res);
?>

我有一个文本框,我希望使用patient_name自动填充库将jquery-ui显示为自动填充。

这是我的jQuery脚本:

  $(document).ready(function()
  {
    $( "#searchTxt" ).autocomplete({
      source: "../php/autoComplete.php"
    });
  })

我可以看到,如果在网络选项卡上输入一个名称,我可以看到返回的数组:

enter image description here

但是在文本框中,我看到自动填充是空的,如下图所示:

enter image description here

它显示2个白色框而不是返回的数组

4 个答案:

答案 0 :(得分:1)

这是一个很好的插件,可用于此类结果:https://twitter.github.io/typeahead.js/

答案 1 :(得分:1)

我认为它是因为您传递的数据,似乎有多列,您需要指定print('Accuracy: ', accuracy.eval({x: mnist.test.images, y: mnist.test.labels})) Google Sheet API v4或者它应该是简单的数组(根据您的代码,它应该是这个像

那样的数据
label

您可以查看有关custom field passing

的更多信息

答案 2 :(得分:1)

Twitter typeahead.js是实现此功能的最佳选择。

请使用PHPAJAXTypeAhead.js

来了解相关信息
<script>
$(function() {
  $("#searchTxt").typeahead({
    source: function(query, process) {
      var textVal=$("#searchTxt").val();
      $.ajax({
        url: '/php/autoComplete.php', // Please add full URL here
        type: 'POST',
        data: 'term=' + textVal,
        dataType: 'JSON',
        async: true,
        success: function(data) {
          process(data);
          console.log(textVal);
        }
      });
    }
  });
});
</script>

链接到TypeAhead.js

如果您需要帮助,请告诉我。

答案 3 :(得分:0)

因为你需要在你的json数组中存储标签和值,所以你的sql会是,

$getPatients = "SELECT *, name as label, id as value FROM patient WHERE clinic_id = :cid  ORDER BY patient_id DESC";