javascript Submit multiple forms with one button

前端 未结 4 1774
迷失自我
迷失自我 2021-01-02 06:46

In my html I have multiple forms (text inputs, radio buttons, check boxes and select) and one button. I would like to fill all these forms and send values to my php file. Fo

相关标签:
4条回答
  • 2021-01-02 07:09

    It will be easier to only submit one form. You can give your select and input tag the same form name by assigning form="your-form-id".

    0 讨论(0)
  • 2021-01-02 07:13

    Once you are submitting one form your page reloads or terminates the javascript after that submission of form so better use Ajax for submitting multiple forms at the same time

    with jquery

    $("#sub").click(function(){
        $("form").each(function(){
            var fd = new FormData($(this)[0]);
            $.ajax({
                type: "POST",
                url: "solve.php",
                data: fd,
                processData: false,
                contentType: false,
                success: function(data,status) {
                   //this will execute when form is submited without errors
               },
               error: function(data, status) {
                   //this will execute when get any error
               },
           });
        });
    });
    

    Above code will submit every form when you click a button with id sub

    0 讨论(0)
  • 2021-01-02 07:18

    Here's an simple example of a native Javascript implementation.

    <!DOCTYPE html>
    <html>
    <head>
        <title>Multiform - JAVASCRIPT</title>
    </head>
    <body>
    
    <fieldset>
        <legend>Form 1</legend>
        <form name="f1" id="f1" onsubmit="return validate(this)">
            <input type="text" name="username" placeholder="Username" />
        </form>
    </fieldset>
    
    <fieldset>
        <legend>Form 2</legend>
        <form name="f2" id="f2" onsubmit="return validate(this)">
            <input type="text" name="email" placeholder="Email" />
        </form>
    </fieldset>
    
    <fieldset>
        <legend>Form 3</legend>
        <form name="f3" id="f3" onsubmit="return validate(this)">
            <input type="text" name="password" placeholder="Password" />
        </form>
    </fieldset>
    
    <button onclick="submitAll();">SUBMIT ALL</button>
    
    <script>
    'use strict';
    
    function validate(form){
        //forms processsing goes here...
        console.log(form, form.name)
        return false;
    }
    
    function submitAll(){
        for(var i=0, n=document.forms.length; i<n; i++){
            document.forms[i].onsubmit();
        }
    
    }
    
    </script>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-02 07:19

    You could try this. If submitting all forms is fine for your page

     $('form').submit();
    

    Function that's in actual use:

    function trySubmitAllForms() {
        if ($('.saveSpinner').is(':visible')) {
            return;
        }
        if ($('form').valid()) {
            $('.saveSpinner').show();
            $('form').submit();
        } else {
            showValidationErrorMessages();
        }
    }
    
    0 讨论(0)
提交回复
热议问题