2017-07-06 99 views
0

我有這個PHP腳本來從我的數據庫獲取所有患者姓名:jQuery的自動完成功能是顯示空的結果

<?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個白框,而不是一個返回數組的

+0

你必須分析使用'JQuery.parseJSON()JSON數組'然後設置每個結果上自動完成表單,它可以是一個可以從解析的json數據構建的數組。 –

+0

我不知道你是什麼意思 – droidnation

+0

你可以嘗試我發佈在這個[pastebin](https://pastebin.com/1s2Ew4YE) – guradio

回答

1

我認爲它是因爲該數據已通過,似乎有多個列,你需要指定labelvalue或者它應該是簡單的數組(按你的代碼應該是這樣的數據),如

var availableTags = [ 
    "ActionScript", 
    "COBOL", 
    "ColdFusion", 

]; 

您CA n查看更多關於custom field passing

1

推特typeahead.js是實現此功能的最佳選擇。

請看看這個與PHPAJAX來實現它,並且TypeAhead.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

讓我知道如果你需要任何幫助。

+0

我添加了'typeahead'庫,並添加了你的腳本和正確的url, t看到什麼 – droidnation

+0

沒有錯誤,也沒有結果 – droidnation

+0

仍然是一樣的PHP腳本,但我將一行改爲:'$ searchTxt ='%'。$ _ POST ['term']。'%';' – droidnation

0

因爲你需要的標籤,並在你的JSON數組提起讓你的SQL將是價值,

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