jqueryformsmultiple-forms

Submit multiple forms to other sites on a single click. (It does not support AJAX.)


I have been using jQuery to submit multiple forms by using this function:

<script>
    var submitted = "";
    function submitAllForms() {               

        var i=0;

        $("form").each(function () {

               submitted +=  $(this).submit();
               alert("submitted:"+i);

              i++;  
        });
    }

</script>';

But, this does not submit to all. It submits and posts data to only one site. In other words, only one form is being posted.


Solution

  • ANSWER UPDATE

    According to the comment:

    It worked on Firefox but doesn't work on Google Chrome

    This issue is related to the dom ready event. This doesn't work in firefox. I tested the changed snippet with the last versions of Chrome, FF, Edge, IE. Only for FF I needed to use the event load instead of ready.

    A possible solution can be based on IFRAMES (if you can use them):

    The snippet:

    function onIframeReady(doc, html) {
      doc.find('body').append(html);
      //
      // if it's needed to change the action atr use:
      // doc.find('form').attr('action', 'new ac');
      //
      doc.find('form').submit();
    }
    
    $(function () {
      $('#submitMoreForms').on('click', function(e){
        $('#frmWrapper').empty();
        $('form').each(function(idx, ele){
          $(ele).find(':input').each(function(index, element) {
            if (element.type == 'textarea') {
              $(element).text($(element).val());
            } else {
              $(element).attr('value', $(element).val());
            }
          });
    
          $('<iframe>', {
            id:  'frm' + idx
          }).appendTo('#frmWrapper').ready(function() {
            if (window.mozInnerScreenX == null) {  // if not firefox
              onIframeReady($('#frm' + idx).contents(), ele.outerHTML);
            }
          }).one('load', function(e) {
            if (window.mozInnerScreenX != null) { // if firefox
              onIframeReady($('#frm' + idx).contents(), ele.outerHTML);
            }
          });
        });
      });
    });
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    
    <form action="action1" method="post">
      FORM1:<br/>
        Name: <input type="text" name="name" />
        Comment: <textarea name="comment"></textarea>
        <input type="checkbox" name="vehicle" value="Bike"> I have a bike
        <input type="checkbox" name="vehicle" value="Car" checked> I have a car
    </form>
    <form action="action2" method="post">
      FORM2:<br/>
        Name: <input type="text" name="name" />
        Comment: <textarea name="comment"></textarea>
    </form>
    <form action="action3" method="post">
      FORM3:<br/>
        Name: <input type="text" name="name" />
        Comment: <textarea name="comment"></textarea>
    </form>
    
    <input id="submitMoreForms" type="button" value="Submit More Forms" />
    <div id="frmWrapper" style="display: none;"></div>