一键提交多个表格 [英] Submit Multiple Forms With One Button

查看:111
本文介绍了一键提交多个表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用$_SESSION为我的网上商店动态创建表单.这些表格包含客户所需产品的自定义信息.这是布局:

I am using $_SESSION to dynamically create forms for my web store. These forms hold the custom info for the product that the customer wants. This is the layout:

第1页

客户填写的表单看起来像这样:

Customer fills out form that looks something like this:

<form action="page2" method="post">
<input type="text" name="size">
<input type="text" name="color">
<input type="submit" name="submit" value="Review Order">
</form>

第2页

客户查看订单详细信息,并可以选择添加更多产品.客户返回第1页订购另一张.客户的所有订单将以其各自的形式显示在第2页上.

Customer reviews order details and has the option of adding more products. Customer goes back to page1 to order another one. All of the customer's orders will show on page2 in their respective form.

看起来像这样:

Size: 1
Color: blue
Click Here To Checkout

Size: 2
Color:green
Click Here To Checkout

Size:3
color:red
Click Here To Checkout

我想要的是一个按钮,它将所有订单添加到PayPal购物车.当然,他们可以通过单击Click Here To Checkout分别添加每个订单,但随后他们将不得不经历一个大循环才能添加多个产品.

What I want is one button that will add ALL orders to the PayPal cart. Sure they can add every order individually by clicking on Click Here To Checkout, but then they will have to go through a big loop to add multiple products.

我希望客户能够添加尽可能多的产品,然后单击一个将所有订单添加到购物车的按钮.

I want the customer to be able to add as many products as possible and then click one button that adds all of the orders to the shopping cart.

这是我尝试过的方法,但显然不起作用:

<script>
$(document).ready(function(){
$('#clickAll').on('click', function() {
    $('input[type="submit"]').trigger('click');
    });
    });
    </script>

    <form action="" method="post">
    <input type="text" name="name">
    <input type="submit" name="submit" value="submit">
    </form>

    <form action="" method="post">
    <input type="text" name="name">
    <input type="submit" name="submit" value="submit">
    </form>

    <form action="" method="post">
    <input type="text" name="name">
    <input type="submit" name="submit" value="submit">
    </form>

    <button id="clickAll">Submit All</button>

以下是使用$_SESSION 生成动态表单的php脚本:

Here is the php script that generates the dynamic forms using $_SESSION:

<?php

if(isset($_POST['submit'])) :

$test = array(
    'size' => $_POST['size'],
    'color' => $_POST['color'],
    'submit' => $_POST['submit']
);

$_SESSION['testing'][] = $test;

endif;


if(isset($_SESSION['testing'])) : 

foreach($_SESSION['testing'] as $sav) {

?>

<form action="paypal.com/..." method="post">
<input type="text" name="size" value="<?php echo $sav['size']; ?>">
<input type="text" name="color" value="<?php echo $sav['color']; ?>">
<input type="submit" name="submit" value="Click Here to Checkout">
</form>

<?php } endif; ?>

所以问题是,如何使用一个按钮提交所有表单?

推荐答案

您是否尝试过使用$ .ajax?您可以添加一个foreach,或在Onsucces函数上调用其他表单.另一种方法是使用指向正确的抽象"表单的数组将所有表单更改为一种表单:

Have you tried to do it with $.ajax? You can add an foreach, or call another form on the Onsucces function. Another approach is changing all to one form with an array that points to the right "abstract" form:

<form action="" method="post">
    <input type="text" name="name[]">
    <input type="text" name="example[]">

    <input type="text" name="name[]">
    <input type="text" name="example[]">

    <input type="text" name="name[]">
    <input type="text" name="example[]">

    <button id="clickAll">Submit All</button>
</form>

在php中:

foreach ($_POST['name'] as $key => $value) {
    $_POST['name'][$key]; // make something with it
    $_POST['example'][$key];  // it will get the same index $key
}

这篇关于一键提交多个表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆