1

I want to submit html forms present in an array. But the problem is only last form is actually posted to server. All other forms do not reach server. Any hint what I am doing wrong here?

if (numberOfRows > 0) {
    for (var i = 0; i < numberOfRows; i++) 
        {
            alert('submitting form ' + i);
                submittedForms[i].submit();
        }
    }
}
Jonas
  • 1,326
  • 2
  • 10
  • 27
Faiza Iqbal
  • 141
  • 10

3 Answers3

4

Submitting a form with .submit() is similar to a user clicking a submit button which will result in a new page being loaded. So calling .submit() on multiple forms in the same page would equal a user clicking multiple submit buttons, what is the browser supposed to be doing then? I'm not sure if the specification documents a certain behavior, but I don't see how this could be handled generally. If all you want is that the form data reaches the server, use AJAX. For example like this when using jQuery: jQuery AJAX submit form

pschichtel
  • 689
  • 7
  • 17
0

Try this:

$('#form1').submit();
$('#form2').submit();
Pang
  • 8,605
  • 144
  • 77
  • 113
0

Thanks to @pschichtel. I ended up using ajax to post multiple forms at once. Below is my final code:

 var numberOfRows = submittedForms.length;
 if (numberOfRows > 0) {
                for (var i = 0; i < numberOfRows; i++) {

                    var formData = new FormData(submittedForms[i]); // yourForm: form selector        
                    postFormAjax(formData);
                }
                deleteForms();
            }
            
function postFormAjax(formData)
{
    $.ajax({
        type: "POST",
        url: "https:url/servlet/servlet.WebToCase?encoding=UTF-8", // where you wanna post
        data: formData,
        processData: false,
        contentType: false,
        error: function (jqXHR, textStatus, errorMessage) {
            alert(errorMessage); // Optional
        },
        success: function (data) {
            alert(data)
        }
    });
}
Faiza Iqbal
  • 141
  • 10