Let's assume I have a table in a form, containing inputs and checkboxes:
<form method='post' action=''>
<table>
<tr>
<th>Code</th>
<th>Description</th>
<th>Select</th>
</tr>
<tr>
<td><input name=table[code][] type='text' value='C001' /></td
<td><input name=table[description][] type='text' value='Object 1' /></td>
<td><input name=table[select][] type='checkbox' checked /></td>
</tr>
<tr>
<td><input name=table[code][] type='text' value='C002' /></td
<td><input name=table[description][] type='text' value='Object 2' /></td>
<td><input name=table[select][] type='checkbox' /></td>
</tr>
<tr>
<td><input name=table[code][] type='text' value='C003' /></td
<td><input name=table[description][] type='text' value='Object 3' /></td>
<td><input name=table[select][] type='checkbox' checked /></td>
</tr>
</table>
</form>
The table is populated by PHP and extended by the user (through a jQuery script), so I cannot know how many rows I'll have when the form is posted
in $_POST I find these values (not PHP code, just showing $_POST content):
$_POST['table']['code'][0] = C001
$_POST['table']['code'][1] = C002
$_POST['table']['code'][2] = C003
$_POST['table']['description'][0] = Object 1
$_POST['table']['description'][1] = Object 2
$_POST['table']['description'][2] = Object 3
$_POST['table']['select'][0] = 1
$_POST['table']['select'][1] = 1
As you can see, I have no correlation between 'select' fields and 'code/description'
My solution was to add the index of the array, like this:
<td><input name=table[code][1] type='text' value='C001' /></td>
<td><input name=table[description][1] type='text' value='Object 1' /></td>
<td><input name=table[select][1] type='checkbox' checked /></td>
and checking if(isset(&_POST['table']['select'][1]))
but this doesn't feel an elegant solution to me, as it require to number the fields as the table is populated and update numbers in jQuery as new rows are added.
Is there a better solution for keeping each row's input together?