2016-03-05 27 views
3

我想填充按鈕單擊的數據表,它會發送一個日期到process.php,這反過來會查詢數據庫並輸出一個json數組。使用ajax從mysql填充jQuery數據表

這裏是我的代碼:

的Javascript:

var dailyCollTable = $("#dailyColl").DataTable(); 

$("#dailyBills").click(function(){ 
    var date = $("#billDate").val(); 
    $.ajax({ 
     type:'post', 
     url:'process.php', 
     data:{date:date}, 
     dataType:'json', 
     success:function(s){ 
      console.log(s); 
      dailyCollTable.fnClearTable(); 
      for(var i = 0; i < s.length; i++){ 
       dailyCollTable.fnAddData([ s[i][0], s[i][1], s[i][2], s[i][3], s[i][4], s[i][5] ]); 
      } 
     } 
    }); 
}); 

這是我process.php

<?php 
$connection = mysqli_connect("localhost", "root", "", "database"); 

$date = $_POST['date']; 

$query = mysqli_query($connection,"SELECT CustomerName,BillNumber,BillDate,BillAmount,PaidAmount,PaymentDate FROM billentry WHERE Status=1 AND PaymentDate='$date'"); 

while($fetch = mysqli_fetch_array($query)){ 
    $output[] = array ($fetch[0],$fetch[1],$fetch[2],$fetch[3],$fetch[4],$fetch[5]); 
} 
echo json_encode($output); 

>

我檢查瀏覽器控制檯?陣列會好起來的。我得到的錯誤是

Uncaught TypeError: dailyCollTable.fnClearTable is not a function

我已經包括了所有必要的庫。

完全相同的代碼在我早期的表上工作。

回答

1

有錯誤在查詢結束...PaymentDate='$date'

你應該寫...PaymentDate='" . $date . "'