2017-01-31 33 views
0

我有表是每一行都有按鈕來顯示更多的細節數據基於數據行用戶點擊(使用數據表),過程是我使用ajax將數據從js文件傳遞到php文件,然後在那個php文件經過一些處理後生成一個數組,然後再次將它返回給js,然後通過1回顯數組1 ..發送php變量作爲數組在php到js文件,然後回顯它的每個變量

我成功地將變量傳遞給了childdatatatable。 php使用datatablescript.js中的ajax。

,但我仍然無法數字如何從childdatatatable.php傳回我產生數組datatablescript.js並顯示(通過警報或回聲或任何東西)

SRY的英語不好..

這裏是代碼:

transactions.php

<table id="transactiontable" class="table table-striped table-bordered display" cellspacing="0" width="99%"> 
    <thead> 
     <tr> 
     <th width="4%"></th> 
     <th width="4%">Order Date</th> 
     <th width="10%">ID Transaksi</th> 
     <th width="7%">User ID</th> 
     <th width="9%">Total</th> 
     <th width="5%">Status</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php 
     $query = mysqli_query($koneksi, "select * from penjualan order by tanggal desc"); 
     while($data = mysqli_fetch_array($query)){ 
     ?> 
     <tr data-child-value="<?php echo $data['no_pen'];?>" id="<?=($data['no_pen'])?>"> 
     <td class="details-control"></td> 
     <td> 
      <center><?php echo $data['tanggal']; ?></center> 
     </td> 
     <td> 
      <center><?php echo $data['no_pen']; ?></center> 
     </td> 
     <td> 
      <center><?php echo $data['id_usr']; ?></center> 
     </td> 
     <td> 
      <center>Rp. <?php echo number_format($data['jumlah_total'],0,'','.'); ?>,-</center> 
     </td> 
     <td> 
      <center><?php echo $data['status']; ?></center> 
     </td> 
     </tr> 
     <?php } ?> 
    </tbody> 
</table> 

datatablescript.js

function format(value) { 
    var id = value; 
    return '<div>Detail Item : ' + value + '</div>'; 


} 

$(document).ready(function() { 
    var table = $('#transactiontable').DataTable({}); 

    // Add event listener for opening and closing details 
    $('#transactiontable').on('click', 'td.details-control', function() { 

     var elem = $(this), 
      selecteditem = elem.val(), 
      id = elem.closest('tr').attr('id'); 

     $.ajax({ 
      type: "post", 
      url: "childdatatable.php", 
      data: { 
       'id': id 
      }, 
      success: function(data) { 
       if (data) { 
        var test = '<?php echo json_encode($brid) ?>'; 
        alert(test); 

       } 

      } 
     }); 
     var tr = $(this).closest('tr'); 
     var row = table.row(tr); 

     if (row.child.isShown()) { 
      // This row is already open - close it 
      row.child.hide(); 
      tr.removeClass('shown'); 
     } else { 
      // Open this row 
      row.child(format(tr.data('child-value'))).show(); 
      tr.addClass('shown'); 
     } 
    }); 
}); 

childdatatatable.php

require_once("koneksi.php"); 
session_start(); 

    if (!isset($_SESSION['username'])){ 
     echo "<script>alert('You must register an account first, we will redirect you to register page !'); window.location = 'registuser.php'</script>"; 
    } 

$no_pen = $_POST['id']; 
$query = "SELECT br_id from item_penjualan where penjualan_id = '$no_pen'"; 

if ($stmt = mysqli_prepare($koneksi,$query)) 
    { 
     mysqli_stmt_execute($stmt); 
     mysqli_stmt_bind_result($stmt,$brid); 

     while (mysqli_stmt_fetch($stmt)){ 
      printf ($brid); 
     } 
     json_encode($brid); 
     mysqli_stmt_close($stmt); 
    } 
+0

一個網絡服務器不會(正常)運行'.js'文件中任何的PHP - 它只是把它直按原樣向客戶提供。無論如何,混合兩者通常是一個糟糕的主意,因爲那麼js不能被緩存。相反,在您的.php頁面中,添加類似''並使用您的.js文件中的'myData'。 –

+0

所以它像改變變量js類型之前發送到js文件的權利? –

回答

0

@childdatatatable.php你編碼陣列後,回聲它。

echo json_encode($brid); 

另外:printf是一個浪費的線,對吧?我的意思是,你沒有對$brid進行任何格式化。

@datatablescript.js你可以告訴的.js期待和解碼你的JSON這樣的:

$.ajax({ 
    type: "post", 
    url: "childdatatable.php", 
    data: {'id': id}, 
    success: function(data){ 
     if(data){ 
      var test = data; 
      //alert(test); 
     } 
    }, 
    dataType:"json" 
}); 
+0

@GustiAldi如果我的答案解決了您的問題,請授予它一個綠色的勾號(並可能upvote它作爲有用的)。否則,請解釋需要解決的問題。 – mickmackusa