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
)
With the help of RiggsFolly, I created this solution, which is very simple.
I changed the form html to this:
<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(
array(
'title' => 'Test 1',
'content' => 'test content'
),
array(
'title' => 'Test 2',
'content' => 'test content'
),
array(
'title' => 'Test 3',
'content' => 'test content'
)
);
foreach( $extras as $index => $extra ) { ?>
<div class="item">
<input type="text" name="extra_content[]" value="<?php echo $extra['title']; ?>">
<input type="text" name="extra_title[]" value="<?php echo $extra['content']; ?>">
</div><!--End .item-->
<?php
}
?>
<input type="submit" name="submit" value="submit">
</form>
The POST outputs this:
Array
(
[extra_title] => Array
(
[0] => Test 4
[1] => Test 1
[2] => Test 2
[3] => Test 3
)
[extra_content] => Array
(
[0] => Tkldmsd
[1] => test content
[2] => test content
[3] => test content
)
[submit] => submit
)
And then you can combine those values by index as follows:
<?php
if( ! empty( $_POST ) ) {
echo '<pre>';
print_r( $_POST );
echo '</pre>';
if( ! empty( $_POST['extra_title'] ) && ! empty( $_POST['extra_content'] ) ) {
$extra = array();
foreach( $_POST['extra_title'] as $index => $title ) {
$extra[$index] = array(
'title' => $title,
'content' => $_POST['extra_content'][$index]
);
}
}
}
?>
you have gavethe key in php created input while it will not work so you have to leave key blank just give its value
this is working code for your need
<form method="post">
<!-- This is for creating a new item -->
<div class="item">
<input type="text" name="extra[0][title]">
<input type="text" name="extra[0][content]">
</div><!--End .item-->
<!-- Items from array -->
<?php
$extras=array(0=>'aa',1=>'bb');
$i=1;
foreach( $extras as $index => $extra ) { ?>
<div class="item">
<input type="text" name="extra[<?php echo $i ?>][title]" value="<?php echo $extra; ?>">
<input type="text" name="extra[<?php echo $i ?>][content]" value="<?php echo $extra; ?>">
</div><!--End .item-->
<?php
$i++;}
?>
<input type="submit" name="submit">
</form>
<?php
if( ! empty( $_POST['extra'] ) ) {
echo '<pre>';
print_r( $_POST['extra'] );
echo '</pre>';
}
?>
It's your code, and it works. Just php now check for "isset".
<?php
if( isset( $_POST ) ) {
echo '<pre>';
print_r( $_POST["extra"] );
echo '</pre>';
}
?>
<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( "alfa","beta" );
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>