Javascript 在php中将php变量作为数组发送到js文件,然后对每个变量进行回显
我有一个表,它是每一行都有按钮来显示基于数据行的更详细的数据用户点击使用datatables,这个过程是我使用ajax将数据从js文件传递到php文件,然后在php文件中,在生成一个数组的某个过程之后,将它再次返回给js,然后按1回显数组 我使用datatablescript.js中的ajax成功地将变量传递给childdatatatable.php 但我仍然不知道如何将我从childdatatatable.php生成的数组传递回datatablescript.js,并通过alert或echo或其他方式显示它 英语不好的原因 以下是代码: transactions.phpJavascript 在php中将php变量作为数组发送到js文件,然后对每个变量进行回显,javascript,php,jquery,arrays,ajax,Javascript,Php,Jquery,Arrays,Ajax,我有一个表,它是每一行都有按钮来显示基于数据行的更详细的数据用户点击使用datatables,这个过程是我使用ajax将数据从js文件传递到php文件,然后在php文件中,在生成一个数组的某个过程之后,将它再次返回给js,然后按1回显数组 我使用datatablescript.js中的ajax成功地将变量传递给childdatatatable.php 但我仍然不知道如何将我从childdatatatable.php生成的数组传递回datatablescript.js,并通过alert或echo或
<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);
}
@childdatatatable.php对数组编码后,对其进行回显
echo json_encode($brid);
还有:这是一条浪费的线路,对吗?我的意思是,你没有对$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"
});
Web服务器通常不会在.js文件中运行任何PHP—它只是按原样直接发送给客户端。总之,将两者混合使用通常是个坏主意,因为这样js就无法缓存。相反,在.php页面中,添加类似var myData=;并使用您的.js文件中的myData。因此,在发送到js文件之前,将变量更改为js类型是吗?@GustiAldi如果我的答案解决了您的问题,请给它一个绿色的勾号,并可能将其作为有帮助的投票。否则,请解释需要修复的内容。