43

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?

danwellman
  • 9,068
  • 8
  • 60
  • 88
  • Possible duplicate of [Multiple inputs with same name through POST in php](http://stackoverflow.com/questions/7880619/multiple-inputs-with-same-name-through-post-in-php) – Ciro Santilli OurBigBook.com May 31 '16 at 21:45
  • 1
    This question is much older than the suggested duplicate, and has more answers. Surely that one is a duplicate of this? – danwellman May 31 '16 at 21:48
  • 1
    Current consensus is to close by "quality": Since "quality" is not measurable, I just go by upvotes. ;-) Likely it comes down to which question hit the best newb Google keywords on the title. – Ciro Santilli OurBigBook.com May 31 '16 at 21:50

4 Answers4

79

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.

Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • ok cool, so each input with a duplicate name should just be name="foo[]"? Not name="foo[0]" or anything? – danwellman Feb 04 '10 at 22:30
  • 3
    @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 Feb 04 '10 at 22:37
8

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!

Daniel Mihai
  • 147
  • 1
  • 6
  • Great answer because I thought you'd have to quote val in the html form. [val] can be a string or integer. Thanks! – Antony Sep 12 '16 at 08:06
6

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
  • 32,000
  • 37
  • 101
  • 123
2

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,200
  • 6
  • 16