Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML input arrays

<input name="foo[]" ... > 

I've used these before, but I'm wondering what it is called and if there is a specification for it?

I couldn't find it in the HTML 4.01 Spec and results in various Google results only call it an "array" along with many PHP examples of processing the form data.

like image 769
gak Avatar asked Jun 18 '09 05:06

gak


People also ask

How do you input an array in HTML?

var input = document. getElementsByName('array[]'); The document. getElementsByName() method is used to return all the values stored under a particular name and thus making input variable an array indexed from 0 to number of inputs.

What are input arrays?

INPUT ARRAY is a special case of the INPUT statement which allows user to input data from a screen array into a program array. INPUT binding clause. The clause that associates the variables with the input fields, It is the only obligatory clause.

How do you define an array in HTML?

Creating an Array Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

How do you store user input into an array?

But we can take array input by using the method of the Scanner class. To take input of an array, we must ask the user about the length of the array. After that, we use a Java for loop to take the input from the user and the same for loop is also used for retrieving the elements from the array.


2 Answers

It's just PHP, not HTML.

It parses all HTML fields with [] into an array.

So you can have

<input type="checkbox" name="food[]" value="apple" /> <input type="checkbox" name="food[]" value="pear" /> 

and when submitted, PHP will make $_POST['food'] an array, and you can access its elements like so:

echo $_POST['food'][0]; // would output first checkbox selected 

or to see all values selected:

foreach( $_POST['food'] as $value ) {     print $value; } 

Anyhow, don't think there is a specific name for it

like image 144
sqram Avatar answered Sep 22 '22 14:09

sqram


As far as I know, there isn't anything on the HTML specs because browsers aren't supposed to do anything different for these fields. They just send them as they normally do and PHP is the one that does the parsing into an array, as do other languages.

like image 30
Paolo Bergantino Avatar answered Sep 22 '22 14:09

Paolo Bergantino