Multiple Inputs With Same Name Through Post in PHP

Multiple inputs with same name through POST in php

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.

Form send data to php with multiple inputs with the same name

There are different possibilities how to do that. One Suggestion (i limit it to the parts which are relevant)

form (note that i gave work_name the id as an index, use_it not (but it could have)

<td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
<input type="hidden" name="work_name[<?php echo $work->id?>]" value="<?php echo $work->name; ?>" />

The form only submits the checked checkboxes values in an array, all other are omited. Therefore we could loop over the checked checkbox values like this

foreach($_POST['useit'] as $work_id){
$work_name = $work_names[$work_id];
//do something with the checked rows only (all others are not looped over)
}

This is only possible, due to the given id as an array key in the form work_name[<?php echo $work->id?>].

A general sidenote for better (and more secure) code: please note that your data could be modified by the user, and send back with wrong data (or worse). So please make sure to sanitize your input, or probably better in this case only submit the id in question and the new data and pickup the rest directly from your database. So you can make sure the hidden data has not been modified on the client side and send back wrong.

multiple inputs of the same name in an html form

If there are duplicated names then the values will be in an array with the name. The demo below sends to a live test server and the response is directed to an <iframe>