无法将数据从订单传递到打印表单ajax

时间:2018-07-23 06:37:33

标签: php json ajax

完成订单后,我需要打印简单的收据,因此当重定向到打印页面时,它将成功重定向,但是数据没有传递到我的问题所在的打印页面。我认为网址有问题。我附上了到目前为止我尝试过的代码。

function addProject(){

var table_data = [];
$('#product_list tbody tr').each(function(row,tr)
{
    var sub = {
        'barcode' : $(tr).find('td:eq(1)').text(),
        'pname' : $(tr).find('td:eq(2)').text(),
        'pro_price' : $(tr).find('td:eq(3)').text(),
        'qty' : $(tr).find('td:eq(4)').text(),
        'total_cost' : $(tr).find('td:eq(5)').text(),

    };
    table_data.push(sub);
});
console.log(table_data);

$.ajax({
    type : "POST",
    url: "add_purchase.php", // add the data to add_purchase.php
    dataType: 'JSON',

    data: { data:table_data},

    success: function (data) {

        console.log(_data);



        $('#save').prop('disabled', false);
        $('#save').html('');
        $('#save').append('Add');


        var msg;



            msg="Oreder Completed";

            window.location.href = "print.php?" + data;


        $.alert({
            title: 'Success!',
            content: msg,
            type: 'green',
            boxWidth: '400px',
            theme: 'light',
            useBootstrap: false,
            autoClose: 'ok|2000'

        });
        isNew = true;

    }

print.php

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "upos";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {


    echo  $relative_list = $_POST['data'];

    echo  "Hi";


    for($x = 0; $x < count($relative_list); $x++)
    {

        $barcode= $relative_list[$x]['barcode'];
        $pro_pricee= $relative_list[$x]['pro_price'];
        $qty= $relative_list[$x]['qty'];
        $total= $relative_list[$x]['total_cost'];
    }
}
?>

<div class="invoice-title" align="right">

    Invoice   <b><?php echo  $barcode;   ?></b>
</div>

此处数据为未定义变量:条形码

Add_purchase.php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $relative_list = $_POST['data'];
    for($x = 0; $x < count($relative_list); $x++)
    {
        $stm = $conn->prepare("INSERT INTO purchase_item(prod_id,buyprice,qty,total)
          VALUES (?,?,?,?,?)");
        $stm->bind_param("issss",$last_id,$prod_id,$buyprice,$qty,$total);
        $prod_id= $relative_list[$x]['barcode'];
        $buyprice= $relative_list[$x]['pro_price'];
        $qty= $relative_list[$x]['qty'];
        $total= $relative_list[$x]['total_cost'];

        if ($stm->execute()) {
            echo 1;
        } else {
            echo $conn->error;
        }

        $stm->close();
}

}

1 个答案:

答案 0 :(得分:0)

像这样将您的代码变形

window.location.href = "print.php?data=" + data;

并将$ _POST转换为$ _GET

if ($_SERVER['REQUEST_METHOD'] == 'GET') {

$relative_list = $_GET['data'];
}