2017-07-03 36 views
2

我正在使用jquery-ui庫將自動完成庫添加到我的項目中。JQuery的自動完成庫返回數組沒有來自PHP腳本的屬性

我創建的PHP腳本,我需要得到的數據:

<?php 
error_reporting(E_ALL); 
ini_set("display_errors", 1); 
require_once('connection.php'); 
$cid = $_SESSION['clinic_id']; 
$searchTxt = '%'.$_POST['searchTxt'].'%'; 
$res = array(); 
$getPatients = "SELECT * FROM patient WHERE clinic_id = :cid and patient_name_en LIKE :searchTxt ORDER BY patient_id DESC"; 
$execGetPatients = $conn->prepare($getPatients); 
$execGetPatients->bindValue(':cid', $cid); 
$execGetPatients->bindValue(':searchTxt', $searchTxt); 
$execGetPatients->execute(); 
$getPatientsResult = $execGetPatients->fetchAll(); 

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

echo json_encode($res); 
?> 

和JavaScript部分是在這裏:

<script> 
$(function() { 
    $("#searchTxt").on('keyup', function(){ 


    searchTxt = $("#searchTxt").val(); 
    $.ajax({ 
     url: '../php/autoComplete.php', 
     data: {searchTxt: searchTxt}, 
     type: 'POST', 
     dataType: 'JSON', 
     success:function(resp) 
     { 
     $.each(resp, function(key, result) 
     { 
      var availableTags = result['patient_name_en']; 
     }); 
     }, 
     error:function(resp) 
     { 
     console.log(resp) 
     } 
    }) 

    }); 
$("#searchTxt").autocomplete({ 
     source: availableTags 
    }); 
}); 
</script> 

我曾在關於jQuery的控制檯以下錯誤:

Maximum call stack size exceeded. 

但現在它不知何故,我不知道爲什麼。

現在在搜索文本框中鍵入後,我得到一個空數組在開發工具或陣列的網絡選項卡,但沒有屬性,沒有什麼是顯示爲附近的文本框中自動完成:

enter image description here

編輯

我改了行PHP到:

$searchTxt = '%'.$_POST['searchTxt'].'%'; 

enter image description here

現在沒有PHP的錯誤,但JavaScript錯誤:

Uncaught ReferenceError: availableTags is not defined 

enter image description here

+0

嘗試在後端側打印陣列並查看數組是否被填充。問題可能出現在您的SQL查詢中。 –

+0

我修正了它現在數據出現在網絡選項卡,但我有以下錯誤,請參閱在一分鐘內編輯 – droidnation

+0

看看這裏[如何從自動完成獲取價值](https://stackoverflow.com/questions/4815330/jquery-ui-autocomplete-with-item-and-id) –

回答

1
$("#searchTxt").autocomplete({ 
    source: availableTags 
    }); 


success:function(resp) 
    { 
    $.each(resp, function(key, result) 
    { 
     var availableTags = result['patient_name_en']; 
    }); 
    }, 

您已經聲明AJAX調用的成功方法裏面availableTags和你想訪問它的範圍之外。

要麼您將availableTags設置爲全局變量,要麼在頂部的某處聲明,以便您可以在兩個位置(用於在ajax成功後重新分配以及在自動填充方法中)訪問它。

+0

好吧,現在出現一個新錯誤:'無法加載資源:服務器響應狀態爲404(未找到) - droidnation 42秒前編輯'和一個鏈接與我在文本框內輸入的內容一樣未找到:'The請求的URL/ncd/pages/Walid在此服務器上找不到。「 – droidnation

+0

這意味着請求的url無法提供請求。請檢查ajax調用url是否正確。 – Shiladitya

+0

它是正確的,我顯示返回的數組 – droidnation

1
$("#searchTxt").autocomplete({ 
     source: availableTags 
    }); 
}); 

這段代碼在你的帖子獲得成功之前得到執行,它是非阻塞的,所以你必須寫它類似的東西。

$(function() { 
    $("#searchTxt").on('keyup', function(){ 


    searchTxt = $("#searchTxt").val(); 
    $.ajax({ 
     url: '../php/autoComplete.php', 
     data: {searchTxt: searchTxt}, 
     type: 'POST', 
     dataType: 'JSON', 
     success:function(resp) 
     { 
     $.each(resp, function(key, result) 
     { 
      var availableTags = result['patient_name_en']; 
      $("#searchTxt").autocomplete({ 
     source: availableTags 
    }); 
}); 

     }); 
     }, 
     error:function(resp) 
     { 
     console.log(resp) 
     } 
    }) 

    }); 
}); 
+0

舊的錯誤消失了,但現在是新的錯誤 – droidnation

+0

'無法加載資源:服務器響應的狀態爲404(Not Found)' – droidnation

+0

,並且與我在文本框內輸入的內容鏈接爲未找到:「The requested在此服務器上找不到URL/ncd/pages/Walid。 – droidnation

相關問題