【发布时间】:2012-11-19 15:19:05
【问题描述】:
我有一个带有产品表的表格。最初表格是空的,新产品通过按下按钮动态插入。当用户按下提交按钮时,我会将他重定向到“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 ?
//...
感谢您的回复。
【问题讨论】:
-
除非您在 javascript 中对表单数据进行一些非常具体的转换,否则为什么要使用 JS?只需像没有 javascript 一样提交表单?
-
我认为您希望从同一个表单提交 2 次可能是正确的吗?一个到preview.php,一个到invoice.php?
-
当您最终想要
$_POST数据时,为什么要将POST数据作为JSON对象?
标签: php ajax json redirect post