I have a series of input that represents different forms:
<form id="myform">
<div id="item1">
<label> Name </label><input name="item1" type="text" value=""/>
</div>
<div id="item2">
<label> Name </label><input name="item2" type="text" value=""/>
</div>
<div id="item3">
<label> Name </label><input name="item3" type="text" value=""/>
</div>
</form>
<script>
$.post("/url/", $("#myform").serialize(), function(){});
</script>
When the post happens, is it guaranteed that I'll receive the data the order it is presented in the form? I've tested it and it does return the item1, item2, item3 in that order, but is it safe to assume that?
It's a tricky question. The serialize
documentation doesn't say anything about order, but does say:
...creates a text string in standard URL-encoded notation.
The RFC for that notation, RFC 1867, states:
Each field of the form is sent, in the order in which it occurs in the form, as a part of the multipart stream.
Empirical results using a really old version of jQuery (v1.2.1) and the current version (v1.11.0) both show the order being preserved.
So I would say even though the jQuery documentation isn't explicit about it, its reference to "standard URL-encoded notation" combined with the RFC and the stabiilty of the behavior indicates you can rely on it. If it were to change (which seems very unlikely, jQuery in general does things in document order), I'd think it could reasonably be considered a bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With