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
2010-02-04 21:41:50
ok cool, so each input with a duplicate name should just be name="foo[]"? Not name="foo[0]" or anything?
danwellman
2010-02-04 22:30:45
@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
2010-02-04 22:37:13
@Gordon thanks will try it out
danwellman
2010-02-04 23:34:05
A:
No, you don't. The HTML reference doesn't even allow duplicated name attributes in the same page.
erenon
2010-02-04 21:41:57
Name does not have to be unique. Consider `input type="radio"` as an example.
Gordon
2010-02-04 21:46:53
@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
2010-02-04 22:01:07
@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
2010-02-04 22:32:55
+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
2010-02-04 21:42:15
+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
2010-02-04 21:47:46