【问题标题】:send php variable as a array in php to js file, then echo it every variable将php变量作为php中的数组发送到js文件,然后回显每个变量
【发布时间】:2017-01-31 04:42:38
【问题描述】:

我有一个表格,每行都有按钮来显示基于数据行用户点击的更多详细数据(使用数据表),过程是我使用 ajax 将数据从 js 文件传递​​到 php 文件,然后在该 php 文件中经过一些生成一个数组的进程再次将其返回给 js,然后将数组 1 逐 1 回显..

我使用 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);
    }

【问题讨论】:

  • 网络服务器(通常)不会在 .js 文件中运行任何 PHP - 它只是将其直接发送到客户端。无论如何混合两者通常是一个坏主意,因为那样js就无法被缓存。相反,在您的 .php 页面中,添加类似 &lt;script&gt;var myData=&lt;?php echo json_encode($brid); ?&gt;;&lt;/script&gt; 的内容并使用 .js 文件中的 myData
  • 所以它就像在发送到 js 文件之前将变量更改为 js 类型对吗?

标签: javascript php jquery arrays ajax


【解决方案1】:

@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"
});

【讨论】:

  • @GustiAldi 如果我的回答解决了您的问题,请给它一个绿色勾号(并可能认为它有帮助)。否则,请说明需要解决的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多