如何用一个按钮提交两个表格? [英] How I can submit two forms with one button?

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

问题描述

我有2个[或更多]表单,我想用一个按钮提交它们。

第一个表格已发布到class1,form2发布到class2;如下所示:



I have 2 [or more] forms that I wanna to submit them with one button.
first form have been post to class1 and form2 posted to class2; like below:

                            <form id="form1" action="class1/add" method="post" >
                                <fieldset>
                                    <legend>test</legend>
                                      <label>name*<small></small></label><input autocomplete="off" type="text" name="name" required="required" />
                                    
                                </fieldset>
                            </form>
                            



                            <form id="form2" action="class2/add" method="post" >
                                <fieldset>
                                    <legend>job</legend>
                                      <label>job *<small></small></label><input autocomplete="off" type="text" name="job" required="required" />
    
                                </fieldset>
                            </form>
                            
                                  
<button class="button button-gray" onclick="submitform()"><span class="accept"></span>ثبت</button>





然后使用此脚本提交表格。





and then submitted forms with this script.

submitform = function(){
    //alert("saa");
document.getElementById("form1").submit();
document.getElementById("form2").submit();
}







我只收到第二张表格。

有人可以帮帮我吗?




but I just receive second form.
Can anybody help me?

推荐答案

<script>
    submitform = function(){
        document.getElementById("form1").submit();
        
        setTimeOut(function() {
            document.getElementById("form2").submit();
        }, 5000);
    }
</script>





Karim,我不确定,我们是否可以这样做或不是,因为在提交第一个表单时,它只是重定向到另一个URL(class1 / add),并且在那里,它找不到form2存在的DOM(如果我们正在尝试上面的代码)。



所以,最好是我们可以使用AJAX提交表格,然后提交第二个表格,一旦我们在第一个表格提交上获得成功。



Karim, I am not sure, whether we can do so or not, as while submitting the first form, it is just redirecting to another URL (class1/add), and where, it is not finding the DOM where form2 present (if we are trying the above code).

So, best if we can use AJAX for submitting the form, and then submit the 2nd form, once we will get success on the first form submit.


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

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