tags:

views:

44

answers:

4

If you have a form that POSTs to a PHP file and there are text inputs in the form with duplicate name attributes, will you still be able to obtain the values of both fields from the $_POST array?

+4  A: 

No. Only the last input element will be sent. 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.

See the HTML reference at Sitepoint.

Gordon
ok cool, so each input with a duplicate name should just be name="foo[]"? Not name="foo[0]" or anything?
danwellman
@danwellman Just `foo[]` will do but you can also use `foo[0]`, `foo[bar]` and even `foo[bar][baz]`. Just try the various combinations on your webserver to see how they arrive in $_POST.
Gordon
@Gordon thanks will try it out
danwellman
A: 

No, you don't. The HTML reference doesn't even allow duplicated name attributes in the same page.

erenon
Name does not have to be unique. Consider `input type="radio"` as an example.
Gordon
@Gordon: It's not just radio buttons, the HTML spec says you can have a checkbox set with the same names as well.
R. Bemrose
@R-Bemrose I didn't say it is limited to radio buttons, did I? Radio buttons are just the only inputs where it makes sense to have the same name attribute without the `[]` because it will switch the selected input on the UI.
Gordon
+1  A: 

Only if the name are array-typed names[] in that case you will be getting an array as the variable in the $_POST variable.

jpabluz
+1  A: 

You have to create an array of them: with name=inputname[] and get with post,request or get

$inputs = $_POST['inputname'];

print_r($inputs);
streetparade