简体   繁体   English

使用一个提交按钮提交多个表单,该按钮还传递帖子数据

[英]Submitting multiple forms with one submit button that also passes post data

I'm building a tool that has multiple forms. 我正在构建一个具有多种形式的工具。 One form per tab, that also allows you to create new tabs, and thus new forms. 每个选项卡一个表单,这也允许您创建新的选项卡,从而创建新的表单。

The way I built each tab is like this 我建立每个标签的方式是这样的

    <div class="tabs_item">
        <h4>Tab01</h4>
        <?php $globalIncrement++; include 'qBox.php';  ?>
    </div> 

qBox.php is a form all on it's own, with ids and classes that increment based on $globalIncrement. qBox.php本身就是一种形式,其ID和类根据$ globalIncrement递增。 (the reason I do this is somewhat unrelated). (我这样做的原因有些无关紧要)。

When the user hits the "submit" button, the form takes all the POST data, and with that we create the results.php page. 当用户单击“提交”按钮时,该表单将获取所有POST数据,并由此创建results.php页面。

Problem is, when you submit, it only takes the POST data from the active tab, and not the POST data from the other tabs (which, again, contain different forms). 问题是,当您提交时,它仅从活动选项卡中获取POST数据,而不从其他选项卡(同样包含不同的表单)中获取POST数据。

I tried to rectify this problem with the following code: 我尝试使用以下代码纠正此问题:

$('#subBtn').click(function(){
$('form').each(function(){
    $(this).submit();
});
});

However, this only partially works. 但是,这仅部分起作用。 The form action is still performed, thus bringing me to results.php. 表单操作仍在执行,因此使我进入了results.php。 However, now, none of the POST data is being retrieved. 但是,现在, 没有任何 POST数据被检索。

How can I submit all these forms, while also passing through the POST data to the next page? 我如何提交所有这些表格,同时还要将POST数据传递到下一页?

Thank you in advance! 先感谢您!

EDIT: 编辑:

Solved it myself. 我自己解决了。 Rather than having a form on every tab, I simply wrapped every tab item inside of an all-encompassing form. 与其在每个选项卡上都没有表单,我只是将每个选项卡项都包裹在一个无所不包的表单中。 Works great. 效果很好。

Thank you to all that answered. 谢谢所有回答。

Use this: 用这个:

$(function() {
    $('#subBtn').click(function(e){
        e.preventDefault();
        $('form').submit();
    });

    $('form').on('submit',function(e) {
        e.preventDefault();
        $.post( this.action, $(this).serialize() );
    });
});

Concept Verification 概念验证

With jQuery, you can create one form that contains every input. 使用jQuery,您可以创建一个包含每个输入的表单。 Something like that : 像这样:

$('#subBtn').click(function(){
    $('<form>', {
        //All your attributes
        action : 'url',
        method : 'POST'
    })
    .append($('form').children().clone(true, true))[0].submit();
});

Suppose you have 2 forms: #form1, #form2 and the main form #form0 with submit button: 假设您有2种形式:#form1,#form2和带有提交按钮的主要形式#form0:

$('#form0').on('submit', function(event) {
    event.preventDefault();

    var xhr1 = $('#form1').ajaxSubmit().data('jqxhr');
    var xhr2 = $('#form2').ajaxSubmit().data('jqxhr');

    $.when(xhr1, xhr2).then(function() {
        $('#form0').submit();
    }, function() {
        alert('Error');
    });
});

Note: you should also include Jquery Form Plugin 注意:您还应该包括Jquery Form Plugin

The problem of form submits being blank is likely because they are hidden because of the way the tabs is set up. 表单提交为空白的问题很可能是由于选项卡的设置方式而将其隐藏。 I've seen this happen before on certain elements that had the parent div set to display:none and the browser thinking that it wasn't a valid form element since the user couldn't see it. 我之前曾在某些将父div设置为display的元素上发生过这种情况:none,浏览器认为这不是有效的form元素,因为用户看不到它。

Try this 尝试这个

$('#subBtn').click(function(){
$('form').each(function(){
    $(this).parents('.tab selector').show();
    $(this).submit();
    $(this).parents('.tab selector').hide();
});
});

Solved it myself. 我自己解决了。 Rather than having a form on every tab, I simply wrapped every tab item inside of an all-encompassing form. 与其在每个选项卡上都没有表单,我只是将每个选项卡项都包裹在一个无所不包的表单中。 Works great. 效果很好。

Thank you to all that answered. 谢谢所有回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM