Following form has repeating input fields. Each field has two hidden inputs and one text input. I want to retrieve the values of each field when posted, so that I can store in the database.
<form action="" method="post">
<?php
$arr = [30, 40, 55];
foreach ($arr as $code) { ?>
<?php
for ($i = 1; $i <= 12; $i++ ) { ?>
<input type="hidden" name="field[code]" value="<?php echo $code; ?>">
<input type="hidden" name="field[month]" value="<?php echo $i; ?>">
<input type="text" name="field[amount]"><?php
}
}
?>
<button name="submit">Submit</button>
</form>
I can't figure out how to run the loop to retrieve all three values of each field. I'm trying the following:
<?php
if( isset( $_POST['submit']) ){
foreach( $_POST['field'] as $field ) { //<- problem
$code = $field['code'];
$month = $field['month'];
$amount = $field['amount'];
//insert into db
}
}
?>