Multiple inputs with same name through POST in php
Solution 1
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.
Solution 2
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.
Solution 3
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
I am a computer science student and a prime number theory hobbyist. I have invented a new prime sieve algorithm based on my findings and am working with a team of fellow students to isolate the patterns in the primes so that they may be found directly. I am also involved in a project I am calling CurriConnect, which focuses on bringing the best of recent web technologies to the table for teacher collaboration on curriculum plan creation and evaluation. Other projects I am working on include a simple flash card program that uses an FFT and autocorrelation to estimate pitch and teach music sight reading, a toy game based on the 2d space shooter genre, and anything else I feel like coding at the moment. I plan to work in a research capacity, though I am open to anything I can become obsessed with and enjoy. In the mean time, I am making an honest attempt to truly and completely crack the prime numbers wide open. You will likely hear about this within the next year or two.
Updated on July 08, 2022Comments
-
Adam almost 2 years
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".
-
Adam over 12 yearsThanks 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.
-
Eric over 12 years@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 -
Adam over 12 yearsHow 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 over 12 yearsGood 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.
-
Eric over 12 years@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 -
rajeev over 11 yearsQuestion 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)?
-
user2799603 about 10 yearsI 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?
-
ashishkumar148 about 9 yearshow to handle this type of thing in angularjs. e.g. stackoverflow.com/questions/29415774/…
-
tashi over 8 yearsInput type email and file are only supported for multiple value with same name. I failed to achieve multiple values.
-
Fi Horan over 7 yearsJust 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}
-
Javid over 7 yearsThis is nonsense to parse it yourself. There's no scenario that this would be needed.
-
Mahsa about 3 yearsAny idea about how can I get access to the old value of these fields when the form is validated and return the error?
-
Nico Haase over 2 yearsPlease 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"