141

Is it possible to get multiple inputs of the same name to post and then access them from PHP? The idea is this: I have a form that allows the entry of an indefinite number of physical addresses along with other information. If I simply gave each of those fields the same name across several entries and submitted that data through post, would PHP be able to access it?

Say, for example, I have five input on one page named "xyz" and I want to access them using PHP. Could I do something like:

    $_POST['xyz'][0]

If so, that would make my life ten times easier, as I could send an indefinite amount of information through a form and get it processed by the server simply by looping through the array of items with the name "xyz".

hakre
  • 193,403
  • 52
  • 435
  • 836
Adam
  • 3,668
  • 6
  • 30
  • 55

5 Answers5

258

Change the names of your inputs:

<input name="xyz[]" value="Lorem" />
<input name="xyz[]" value="ipsum"  />
<input name="xyz[]" value="dolor" />
<input name="xyz[]" value="sit" />
<input name="xyz[]" value="amet" />

Then:

$_POST['xyz'][0] == 'Lorem'
$_POST['xyz'][4] == 'amet'

If so, that would make my life ten times easier, as I could send an indefinite amount of information through a form and get it processed by the server simply by looping through the array of items with the name "xyz".

Note that this is probably the wrong solution. Obviously, it depends on the data you are sending.

Eric
  • 95,302
  • 53
  • 242
  • 374
  • Thanks for the help. Since this is the most complete answer to my question, I choose it as THE answer. However, the answer from Interstellar_coder is good as well, but you've explained that this can potentially be a pitfall if used incorrectly. I will be limiting the maximum number of items and I will be doing a lot of validation on this form, so I shouldn't run into any security issues unless there are issues inherent in this approach which have not been explained here. – Adam Oct 25 '11 at 02:14
  • 13
    @Adam: Having reread your question, this is a perfectly accetable approach. You might want to go one stage further, and have `address[0][street]` `address[0][city]` `address[0][zip]`, `address[1][street]` `address[1][city]` `address[1][zip]` ... You can read these with `$_POST['address'][0]['city']`, for instance – Eric Oct 25 '11 at 13:03
  • How would this then be named inside HTML? That would potentially ease the difficulty by quite a lot. The approach I was going to take is have several fields named like so: l1[], l2[], city[], state[], zip[], zip4[]. Then, for the first address, I simply use $i == 0 in each field. So if I understand your approach correctly, I would probably name my elements like so: name="address[][city]" for a city field. Is that correct? – Adam Oct 25 '11 at 16:04
  • @Adam: Possibly. I think you might need to place the index inside the first `[]` though, such as `[0]`. That makes the HTML harder to generate, but the data is no harder to read PHP-side – Eric Oct 25 '11 at 20:30
  • Question coming to my mind here is: whether the field will go in random order inside the array? or will they be in order in which they are laid out in form (even if some fields are left empty)? – rajeev Jan 16 '13 at 15:59
  • I tried this solution in my webpage, but the values "Lorem", "ipsum" will show up on the webpage. I want to hide these values and treat them as IDs, not displayed text. Any suggestions? – user2799603 Feb 18 '14 at 19:22
  • how to handle this type of thing in angularjs. e.g. http://stackoverflow.com/questions/29415774/handling-dynamically-added-form-data-in-angularjs – ashishkumar148 Apr 05 '15 at 21:05
  • Input type email and file are only supported for multiple value with same name. I failed to achieve multiple values. – tashi Jan 16 '16 at 08:00
  • Any idea about how can I get access to the old value of these fields when the form is validated and return the error? – Mahsa May 18 '21 at 09:30
40

In your html you can pass in an array for the name i.e

<input type="text" name="address[]" /> 

This way php will receive an array of addresses.

aziz punjani
  • 25,586
  • 9
  • 47
  • 56
  • 3
    Good and correct answer, which is why I voted it up. You didn't explain much about it though, which is why I didn't select it as the answer to my question. This helps me a ton, as I've been trying to figure this one out for a while (as you may notice in one of my previous questions). My previous approach was to simply add all of the inputs to hidden fields with * separated values and resetting the input fields, but this approach is ten times better as I will be able to allow the deletion of previously added addresses if it is discovered they are incorrect. – Adam Oct 25 '11 at 16:17
  • Just be careful to remember that an empty array will have a count of 1 having been POST'ed with nothing at the '0' index. You'll need to check by using the 'empty' keyword if(!empty($arrayName)){//do something} – Fi Horan Aug 26 '16 at 13:16
29

Eric answer is correct, but the problem is the fields are not grouped. Imagine you have multiple streets and cities which belong together:

<h1>First Address</h1>
<input name="street[]" value="Hauptstr" />
<input name="city[]" value="Berlin"  />

<h2>Second Address</h2>
<input name="street[]" value="Wallstreet" />
<input name="city[]" value="New York" />

The outcome would be

$POST = [ 'street' => [ 'Hauptstr', 'Wallstreet'], 
          'city' => [ 'Berlin' , 'New York'] ];

To group them by address, I would rather recommend to use what Eric also mentioned in the comment section:

<h1>First Address</h1>
<input name="address[1][street]" value="Hauptstr" />
<input name="address[1][city]" value="Berlin"  />

<h2>Second Address</h2>
<input name="address[2][street]" value="Wallstreet" />
<input name="address[2][city]" value="New York" />

The outcome would be

$POST = [ 'address' => [ 
                 1 => ['street' => 'Hauptstr', 'city' => 'Berlin'],
                 2 => ['street' => 'Wallstreet', 'city' => 'New York'],
              ]
        ]
Adam
  • 25,960
  • 22
  • 158
  • 247
0

For anyone else finding this - its worth noting that you can set the key value in the input name. Thanks to the answer in POSTing Form Fields with same Name Attribute you also can interplay strings or integers without quoting.

The answers assume that you don't mind the key value coming back for PHP however you can set name=[yourval] (string or int) which then allows you to refer to an existing record.

Community
  • 1
  • 1
Antony
  • 3,875
  • 30
  • 32
-1

It can be:

echo "Welcome".$_POST['firstname'].$_POST['lastname'];
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 3
    Please share more details. How does this answer the given question? You don't even reference how this helps to use "an indefinite number of physical addresses" – Nico Haase Oct 25 '21 at 09:16