我成功使用datatablescript.js中的ajax将变量传递给childdatatatable.php。
但是我仍然无法想象如何将我从childdatatatable.php生成的数组传回datatablescript.js并显示它(通过警报或回声或任何东西)
因为英语不好而受伤..
这是代码:
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 :(得分: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"
});