PHP进程$\u POST数组
以下表单具有重复的输入字段。每个字段有两个隐藏输入和一个文本输入。我希望在发布时检索每个字段的值,以便存储在数据库中PHP进程$\u POST数组,php,forms,post,Php,Forms,Post,以下表单具有重复的输入字段。每个字段有两个隐藏输入和一个文本输入。我希望在发布时检索每个字段的值,以便存储在数据库中 <form action="" method="post"> <?php $arr = [30, 40, 55]; foreach ($arr as $code) { ?> <?php for ($i = 1; $i <= 12;
<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>
我不知道如何运行循环来检索每个字段的所有三个值。我正在尝试以下方法:
<?php
if( isset( $_POST['submit']) ){
foreach( $_POST['field'] as $field ) { //<- problem
$code = $field['code'];
$month = $field['month'];
$amount = $field['amount'];
//insert into db
}
}
?>
这可能就是您正在寻找的:
<?php
// ...
if (isset( $_POST['submit']) && array_key_exists('field', $_POST)) {
$code = $_POST['field']['code'];
$month = $_POST['field']['month'];
$amount = $_POST['field']['amount'];
//insert into db
}
或者如果你认为这更容易阅读:
<?php
// ...
if (isset( $_POST['submit']) && array_key_exists('field', $_POST)) {
$field = &$_POST['field'];
$code = $field['code'];
$month = $field['month'];
$amount = $field['amount'];
//insert into db
}
这可能就是您正在寻找的:
<?php
// ...
if (isset( $_POST['submit']) && array_key_exists('field', $_POST)) {
$code = $_POST['field']['code'];
$month = $_POST['field']['month'];
$amount = $_POST['field']['amount'];
//insert into db
}
或者如果你认为这更容易阅读:
<?php
// ...
if (isset( $_POST['submit']) && array_key_exists('field', $_POST)) {
$field = &$_POST['field'];
$code = $field['code'];
$month = $field['month'];
$amount = $field['amount'];
//insert into db
}
然后,转储$_POST
然后,转储$_POST 为每个输入字段分配一个数字
<?php
$arr = [30, 40, 55];
foreach ($arr as $code) { ?>
<?php
for ($i = 1; $i <= 12; $i++ ) { ?>
<input type="hidden" name="field[<?php echo $i ?>][code]" value="<?php echo $code; ?>">
<input type="hidden" name="field[<?php echo $i ?>][month]" value="<?php echo $i; ?>">
<input type="text" name="field[<?php echo $i ?>][amount]"><?php
}
}
?>
在后端
<?php
if( isset( $_POST['submit']) ){
foreach( $_POST['field'] as $key => $field ) {
$code = $field['code'];
$month = $field['month'];
$amount = $field['amount'];
//insert into db
}
}
?>
为每个输入字段分配一个数字
<?php
$arr = [30, 40, 55];
foreach ($arr as $code) { ?>
<?php
for ($i = 1; $i <= 12; $i++ ) { ?>
<input type="hidden" name="field[<?php echo $i ?>][code]" value="<?php echo $code; ?>">
<input type="hidden" name="field[<?php echo $i ?>][month]" value="<?php echo $i; ?>">
<input type="text" name="field[<?php echo $i ?>][amount]"><?php
}
}
?>
在后端
<?php
if( isset( $_POST['submit']) ){
foreach( $_POST['field'] as $key => $field ) {
$code = $field['code'];
$month = $field['month'];
$amount = $field['amount'];
//insert into db
}
}
?>
步骤1 var\u dump$\u POST-查看实际通过的内容步骤1 var\u dump$\u POST-查看实际通过的内容