私はすべての行にデータの行のユーザーのクリック(データテーブルを使用)に基づいてより詳細なデータを表示するボタンを持っているテーブルを持っている、プロセスは私が使用していますajaxはjsファイルからphpファイルにデータを渡し、配列を生成しているプロセスの後にphpファイルでjsに返す。phpの変数をPHPの配列としてjsファイルに送り、すべての変数をエコーする
変数をchilddatatatableに渡すのに成功した。 datatablescript.jsのajaxを使用してPHPを実行します。
が、私の悪い英語のための(警告やエコーか何か経由)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);
}
ウェブサーバは、(通常は) '.js'ファイル内の任意のPHPを実行しません - それだけでまっすぐに送信しますそのままクライアントに送信します。とにかく2つを混在させるのは一般的には悪い考えです。なぜならjsはキャッシュに入れられないからです。代わりに、.phpページに ' 'を呼び出し、.jsファイルから' myData'を使用します。 –
jsファイルに送信する前にjs型に変数を変更するのが好きですか? –