简体   繁体   中英

Loop through and submit multiple forms without reloading page using javascript

I have a page of around 40 forms that all need submitted when the "Save All" button is pressed. The problem is that when I try and loop through the forms and submit them only the first form gets submitted. How would I refactor this so that all the forms on the page get submitted? I realize this question has been asked before and that the likely solution has to do with AJAX but I'm just not sure how to implement such a thing.

The Javascript I have so far looks like this:

function SaveAllForms() {
    document.getElementById("SaveAll").innerText = "Processing..";
    var forms = document.forms;
    for (var f = 0, fLen = forms.length; f < fLen; f++) {
        var form = forms[f];
        form.submit();
    }
    window.location.reload();
}

And all the forms look something like this. I'm only including one because like I said, there are around 40 of them on the page:

<form action="/FormSubmitLocation" method="post" name="Feed3">    <tr>

    <td>
        <input type="hidden" name="PREcost" value="3.027" />
        <input type="hidden" name="PREtaxadd" value="4.5" />
        1         
        <input id="Zone" name="Zone" type="hidden" value="1         " />
    </td>
    <td>
        SomeZone
        <input id="ZoneDescription" name="ZoneDescription" type="hidden" value="SomeZone" />
    </td>
    <td>
        SomeCity
        <input id="Rack" name="Rack" type="hidden" value="SomeCity" />
    </td>
    <td>
        SomeProduct
        <input id="ActualProduct" name="ActualProduct" type="hidden" value="SomeProduct" />
    </td>
    <td>
        3.027
        <input data-val="true" data-val-number="The field Cost must be a number." id="Cost" name="Cost" type="hidden" value="3.027" />
    </td>
    <td>
        0.69
        <input id="Tax" name="Tax" type="hidden" value="0.686488  " />
    </td>
    <td>
        <input class="form-control" data-val="true" data-val-number="The field DMarkup must be a number." id="DMarkup" name="DMarkup" onchange="calcNewMarkup(this.value,&#39;PRE&#39;,&#39;Feed3&#39;);" onfocus="javascript:if (this.value!=&#39;&#39;){calcNewMarkup(this.value,&#39;PRE&#39;,&#39;Feed3&#39;)};" style="width: 70px;" type="text" value="69.58" />
    </td>
    <td>
        <input Class="PREDretail form-control" id="DSell" name="DSell" onchange="calcNewMargin(this.value,&#39;PRE&#39;,&#39;Feed3&#39;);" style="width: 70px;" type="text" value="3.859     " />
    </td>
    <td>
        <input class="form-control" data-val="true" data-val-number="The field RMarkup must be a number." id="RMarkup" name="RMarkup" onchange="calcNewMarkupRemote(this.value,&#39;PRE&#39;,&#39;Feed3&#39;);" onfocus="javascript:if (this.value!=&#39;&#39;){calcNewMarkupRemote(this.value,&#39;PRE&#39;,&#39;Feed3&#39;)};" style="width: 70px;" type="text" value="69.67" />
    </td>
    <td>
        <input Class="PRERretail form-control" data-val="true" data-val-number="The field RSell must be a number." id="RSell" name="RSell" onchange="calcNewMarginRemote(this.value,&#39;PRE&#39;,&#39;Feed3&#39;);" onfocus="javascript:if (this.value!=&#39;&#39;){calcNewMarginRemote(this.value,&#39;PRE&#39;,&#39;Feed3&#39;)};" style="width: 70px;" type="text" value="3.8599" />
    </td>
    <td>
        <small id="Update3">3/1/2020 7:57:00 PM</small>
        <input data-val="true" data-val-date="The field DateUpdated must be a date." id="DateUpdated" name="DateUpdated" type="hidden" value="3/1/2020 7:57:00 PM" />
    </td>
    <td>
        <input data-val="true" data-val-number="The field cpid must be a number." data-val-required="The cpid field is required." id="cpid" name="cpid" type="hidden" value="641" />
        <input data-val="true" data-val-number="The field orpid must be a number." data-val-required="The orpid field is required." id="orpid" name="orpid" type="hidden" value="6" />
        <button type="submit" class="btn btn-default" id="Feed3">Update</button>
    </td>
</tr>

Any examples of how to get this to work would be greatly appreciated.

you dont even need forms for ajax requests... its an old way to send data.

Anyway you should prevent the default behaviour of the forms:

form.preventDefault();

You can submit forms with a target, including iframes. You don't need JavaScript to do this outside of invoking submit() as you are. For example

<form target="iframe1_name" ...> ... </form>
<iframe name="iframe1_name" src="about:blank" style="display:none"></iframe>

You can repeat this as often as you want, each form will submit to their hidden iframe.

https://jsfiddle.net/efdmsao8/

Side Note: you may want to avoid calling that reload as it would cancel any pending submits, or wrap it in a timeout to allow them to finish before it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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