I've got an HTML form that allows a user to submit details about one or more children. Each child can have one or more items.
My HTML form structure looks like this:
<input type="text" name="child[1][name]">
<input type="text" name="child[1][dob]">
I then have a table display for the items (as there are 2 fields per item) where the user can add/remove rows via Javascript. Example:
<table>
<tr>
<td><input type="text" name="child[1][item][name]"></td>
<td><input type="text" name="child[1][item][value]"></td>
</tr>
</table>
When I access this data, I'm inputting the child data into a table, then I need to access the items per child separately, so they can be stored in an 'items' table (with the child ID which I'll grab from the database insert).
In my PHP, I'm looking to achieve something like:
foreach($_POST['child'] as $child) {
$child_name = $child['name'];
$child_dob = $child['dob'];
// insert child data to children table
foreach($_POST['item'] as $item) {
$item_name = $item['name'];
$item_value = $item['value'];
// insert item data to items table
}
}
My problem is that without the items, the child array looks fine when I print_r($_POST['child']). However when I include the items as per my HTML above, the array only outputs the last item added (whether there is one or more). I'm not sure if I'm correctly specifying the array for items in the input tags, or how I should then access in the PHP.
If anyone has any suggestions about where my syntax may be wrong, or if perhaps I'm approaching this in the wrong way, that would be much appreciated.