【问题标题】:jQuery post and redirect phpjQuery 发布和重定向 php
【发布时间】: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


【解决方案1】:

当您最终想要 $_POST 数据时,为什么要将数据作为 JSON 对象发布?

但是,如果您愿意这样做,可以使用AJAX。这仅在您想在执行一些 PHP(服务器端)代码后返回客户端代码时使用。

【讨论】:

  • 实际上我需要发送不在“invoice.php”中的数据。这些数据是在页面加载后建立在js端的。有没有办法在发出发布请求之前添加更多数据?
  • Actually I need to send data which is not in the "invoice.php" 是什么意思?不是invoice.php中形成的数据(JSON或POST)吗?
  • 在我的“invoice.php”中,我的 products 表默认只包含一行,其中有一个 。用户可以动态添加其他行。由于从“invoice.php”发布时我需要为我的数据设置一个唯一名称,当我的数据中有多个产品时,如何处理这些选择?
  • 这就是为什么我认为我应该构建一个自定义 json 数据,然后将其发布到“preview.php”,如我上面的示例中提到的那样。
  • 您可以通过动态添加名称(当用户添加行时)来处理selects。之后,当您POSTinvoice.php 页面时,您可以通过通常的 $_POST 数组访问它。
猜你喜欢
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多