我有一个带有产品表的表格。最初表格是空的,新产品通过按下按钮动态插入。当用户按下提交按钮时,我会将他重定向到“preview.php”页面。但首先我需要在函数中构建一个自定义 JSON 对象,使用该对象发出 POST 请求,然后重定向到“preview.php”。那可能吗 ?执行此操作的更好方法是什么?
这是我的文件示例:
//------------
//invoice.php
//------------
<form id="newInvoice" action="preview.php">
<table>...</table>
<button type="submit">Preview</button>
</form>
//-----------
//invoice.js
//-----------
$('#newInvoice').submit(function() {
var data = [
"invoicenumber": code,
"client": {"firstname":"", "lastname":"", ...},
"products": [{...}, {...}, {...}], //multiple products
//building the rest of my data here
];
// I don't know what to do here.
// A call to $.post would send my data asynchronously but I want
// to send the data as I would do from invoice.php and be redirected
// to preview.php.
});
//------------
//preview.php
//------------
$data = json_decode($_POST['data']); // is this correct ?
//...
谢谢你的回复。