How to post an array of undefined length in HTML form element?

To post an array of undefined length in HTML form element. We need different input element of form with a similar name. These elements will have square brackets in the end.

Square brackets make them an array when the form is posted.

eg:- <input type=”text” name=”key_name[]“>

<form>
   <input type="text" name="key_name[]">
   <input type="text" name="key_name[]">
   <input type="text" name="key_name[]">
   <input type="text" name="key_name[]">
   <input type="submit" name="submit">
</form>

The above form will return an array of length 4, with the element in the order of declaration.

After the form is Submitted the fields with a name like “key_name[]” will be converted into a numeric array named key_name.

Leave a Reply

Your email address will not be published. Required fields are marked *