Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP进程$\u POST数组_Php_Forms_Post - Fatal编程技术网

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-查看实际通过的内容