Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSTing Form Fields with same Name Attribute

Tags:

post

php

If you have a form containing text inputs with duplicate name attributes, and the form is posted, will you still be able to obtain the values of all fields from the $_POST array in PHP?

like image 767
danwellman Avatar asked Feb 04 '10 21:02

danwellman


People also ask

Is it possible to include two controls with the same names on the same form?

Yes. More, it is essential if you are dealing with radio button groups.

Can a form have a name attribute?

The name attribute specifies the name of a form. The name attribute is used to reference elements in a JavaScript, or to reference form data after a form is submitted.

Which attribute specifies the name to be used for the field in the form?

Definition and Usage The name attribute specifies the name of an <input> element. The name attribute is used to reference elements in a JavaScript, or to reference form data after a form is submitted. Note: Only form elements with a name attribute will have their values passed when submitting a form.

Which of the following form attribute allows posting of user input to server?

The method attribute of the form element tells the web browser how to send form data to a server. Specifying a value of POST means the browser will send the data to the web server to be processed.


2 Answers

No. Only the last input element will be available.

If you want multiple inputs with the same name use name="foo[]" for the input name attribute. $_POST will then contain an array for foo with all values from the input elements.

<form method="post">     <input name="a[]" value="foo"/>     <input name="a[]" value="bar"/>     <input name="a[]" value="baz"/>     <input type="submit" /> </form> 

See the HTML reference at Sitepoint.

The reason why $_POST will only contain the last value if you don't use [] is because PHP will basically just explode and foreach over the raw query string to populate $_POST. When it encounters a name/value pair that already exists, it will overwrite the previous one.

However, you can still access the raw query string like this:

$rawQueryString = file_get_contents('php://input')) 

Assuming you have a form like this:

<form method="post">     <input type="hidden" name="a" value="foo"/>     <input type="hidden" name="a" value="bar"/>     <input type="hidden" name="a" value="baz"/>     <input type="submit" /> </form> 

the $rawQueryString will then contain a=foo&a=bar&a=baz.

You can then use your own logic to parse this into an array. A naive approach would be

$post = array(); foreach (explode('&', file_get_contents('php://input')) as $keyValuePair) {     list($key, $value) = explode('=', $keyValuePair);     $post[$key][] = $value; } 

which would then give you an array of arrays for each name in the query string.

like image 82
Gordon Avatar answered Sep 26 '22 10:09

Gordon


Instead of name="nfo[val]" just use name="nfo[val][]" and in PHP you can use a foreach()

HTML code:

<form method="post">     <input name="nfo[val][]" value="val1"/>     <input name="nfo[val][]" value="val2"/>     <input name="nfo[val][]" value="val3"/>     <input type="submit" /> </form> 

PHP code:

$output=''; foreach ($nfo['val'] as $key=>$val) {     $output.= $val.", "; } 

$output will be: val1, val2, val3

Hope this helps!

like image 21
Daniel Mihai Avatar answered Sep 23 '22 10:09

Daniel Mihai