php 一键提交多个表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20667765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:24:22  来源:igfitidea点击:

Submit Multiple Forms With One Button

phpjquerysessionpaypalshopping-cart

提问by Josan Iracheta

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

我正在$_SESSION为我的网上商店动态创建表单。这些表单包含客户想要的产品的自定义信息。这是布局:

Page1

第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

第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.

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

Looks like this:

看起来像这样:

Size: 1
Color: blue
Click Here To Checkout

Size: 2
Color:green
Click Here To Checkout

Size:3
color:red
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.

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

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.

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

This is what I tried but it obviously didn't work:

这是我尝试过的,但显然没有用

<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>

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

这是使用以下方法生成动态表单的 php 脚本$_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; ?>

So the question is, how can I submit all of the forms with ONE button?

所以问题是,我怎样才能用一个按钮提交所有表格?

回答by sagits

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:

你有没有试过用 $.ajax 来做?您可以添加一个 foreach,或在 Onsucces 函数上调用另一个表单。另一种方法是使用指向正确“抽象”形式的数组将所有内容更改为一种形式:

<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>

And in php:

在 php 中:

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

回答by Dirty Bird Design

FWIW, I do this by creating an iframe, making that the target for the second form then submit both like this

FWIW,我通过创建一个 iframe 来做到这一点,使其成为第二个表单的目标,然后像这样提交

//create the iframe
$('<iframe id="phantom" name="phantom">').appendTo('#yourContainer');

and create the dual submit like this:

并像这样创建双重提交:

function dualSubmit() {
    document.secondForm.target = 'phantom';
    document.secondForm.submit();
    document.firstForm.submit();
}

works!

作品!

回答by drascom

first create loop get all forms id and send them to ajax.

首先创建循环获取所有表单 ID 并将它们发送到 ajax。

<script name="ajax fonksiyonlar?" type="text/javascript">
            function validate(form){
            //get form id
            var  formID = form.id;
            var formDetails = $('#'+formID);
                $.ajax({
                    type: "POST",
                    url: 'ajax.php',
                    data: formDetails.serialize(),
                    success: function (data) {  
                        // log result
                        console.log(data);
                        //for closing popup
                          location.reload();
                        window.close()
                    },
                    error: function(jqXHR, text, error){
                    // Displaying if there are any errors
                    console.log(error);
                    }
                });
            return false;
        }
            //this function will create loop for all forms in page
            function submitAll(){
                    for(var i=0, n=document.forms.length; i<n; i++){
                        validate(document.forms[i]);
                    }
                }

create button for submit in order

创建按钮以按顺序提交

<a class="btn" id="btn" onclick="submitAll();" href="">Save &amp; Close</a>

then stop ajax call after success.also dont forget to log to console.

然后在成功后停止 ajax 调用。也不要忘记登录到控制台。

this code works in popup and closing popup after all ajax completed.

此代码在所有 ajax 完成后在弹出窗口和关闭弹出窗口中工作。

回答by FastTrack

Here is a working jsFiddle: http://jsfiddle.net/SqF6Z/3/

这是一个有效的 jsFiddle:http: //jsfiddle.net/SqF6Z/3/

Basically, add a classto each formand trigger()a submit on that class. Like so:

基本上,class向 each添加一个formtrigger()在其上提交一个class。像这样:

HTML (example only):

HTML(仅示例):

<form action="http://www.google.com" method="get" class="myForms" id="1stform">
    <input type="text" value="1st Form" name="q1" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="2ndform">
    <input type="text" value="2nd Form" name="q2" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="3rdform">
    <input type="text" value="3rd Form" name="q3" />
</form>
<input type="button" id="clickMe" value="Submit ALL" />

jQuery:

jQuery:

$('.myForms').submit(function () {
    console.log("");
    return true;
})

$("#clickMe").click(function () {
    $(".myForms").trigger('submit'); // should show 3 alerts (one for each form submission)
});