Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, as a standard is not necessarily spelled out.
Three possible ways to send multi-value fields or arrays would be:
On a form, multi-valued fields could take the form of a select box set to multiple:
<select multiple="multiple" name="cars">
multiple hidden fields with the same name
<input type="hidden" name="cars" value="Volvo">
<input type="hidden" name="cars" value="Saab">
<input type="hidden" name="cars" value="Mercedes">
NOTE: Using field for multiple values is really poorly documented. I don't see any mention of it in the section on multi-valued keys in Query string - Wikipedia, or in the W3C docs dealing with multi-select inputs.
As commenters have pointed out, this is very much framework-specific. Some examples:
One more thing to consider is that if you need to maintain the order of your items (i.e. array as an ordered list), you really only have one option, which is passing a delimited list of values, and explicitly converting it to an array yourself.
I think the reason that this functionality is not documented in general sources like Wiki or W3C is that it is not generally supported by web frameworks. PHP and I think Rails automatically convert multiple "key" query parameters into an array. Others don't.
The nodejs querystring module uses the ?cars=Saab&cars=Audi form
In express (node.js), both ?cars=Saab&cars=Audi and ?cars=Saab&cars=Audi get turned into arrays. However, ?cars=Saab ends up as a string, but ?cars=Saab is an array with a single element.
Can you set keys using this approach? And what about multi-dimensional arrays?
ASP.NET MVC doesn't appear to read the format ?cars=Saab&cars=Audi There have either be no square brakets or the square brackets need indexes/
Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).