简体   繁体   English

将php变量作为php中的数组发送到js文件,然后将每个变量回显

[英]send php variable as a array in php to js file, then echo it every variable

I have table which is every row has button to show more detail data based on data row user click (using datatables), the process is I use ajax to pass data from js file to php file then in that php file after some process which is generating an a array return it again to js then echo the array 1 by 1.. 我有一个表格,每行都有一个按钮,以根据用户点击数据行(使用数据表)显示更多详细数据,过程是我使用ajax将数据从js文件传递到php文件,然后经过某些过程在那个php文件中传递生成一个数组,将其再次返回给js,然后以1回显数组。

I succeed passing a variable to childdatatatable.php using ajax from datatablescript.js. 我成功地使用了来自datatablescript.js的ajax将变量传递给childdatatatable.php。

but I still cant figure how to pass back the array I generate from childdatatatable.php to datatablescript.js and show it (via alert or echo or anything) 但是我仍然不知道如何将我从childdatatatable.php生成的数组传递回datatablescript.js并显示出来(通过警报或回声或其他方式)

sry for bad english.. 对不起,英语不好。

here's code : 这是代码:

transactions.php transaction.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 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 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 after you encode your array, echo it. 对数组进行编码后,@ @ childdatatatable.php ,将其回显。

echo json_encode($brid);

Also: printf is a wasted line, right? 另外: printf是浪费的行,对吗? I mean, you aren't doing any formatting to $brid . 我的意思是,您没有对$brid进行任何格式化。

@ datatablescript.js you can tell .js to expect and decode your json like this: @ 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"
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM