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

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

问题描述

我正在使用 $_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>

Page2

客户查看订单详细信息并可以选择添加更多产品.客户返回第 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 购物车的按钮.当然他们可以通过点击点击这里结帐单独添加每个订单,但是他们将不得不经历一个大循环才能添加多个产品.

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天全站免登陆