I am saving content items with a title and content value in a database. In my example you can see that I have one item for creating a new content item and the earlier saved items.
I save the items in a multidimensional array by adding the index in each input's name field like <input name="extra[2][title]">
(see example).
For the creating fields I try to store a new item with a new key by doing <input name="extra[][title]">
, which normally pushes a new item to the end of an array. But when I print out the POST data the new item is not there, so I cannot store it in the array with the other items.
HTML FORM:
<form method="post">
<!-- This is for creating a new item -->
<div class="item">
<input type="text" name="extra[][title]">
<input type="text" name="extra[][content]">
</div><!--End .item-->
<!-- Items from array -->
<?php
$extras = array( /* Earlier saved items */ );
foreach( $extras as $index => $extra ) { ?>
<div class="item">
<input type="text" name="extra[<?php echo $index; ?>][title]">
<input type="text" name="extra[<?php echo $index; ?>][content]">
</div><!--End .item-->
<?php
}
?>
<input type="submit" name="submit">
</form>
After doing this, the new item is not there:
<?php
if( ! empty( $_POST['extra'] ) ) {
echo '<pre>';
print_r( $_POST['extra'] );
echo '</pre>';
}
?>
print_r( $_POST ) returns:
Array
(
[extra] => Array
(
[0] => Array
(
[title] => Test 1
[content] => test content
)
[1] => Array
(
[content] => test content
[title] => Test 2
)
[2] => Array
(
[title] => Test 3
[content] => test content
)
)
[submit] => submit
)