如何处理Dynamic Multidimensional输入名称(PHP)的Post值

问题描述 投票:0回答:1

我有一个有几个部分的表单,每个部分有几个步骤。每个步骤至少有一个复选框,最多两个,并且可以有一个注释文本区域。

我正在从DB加载整个表单(问题),我已经创建了一个循环来完成它。我也在通过循环时命名输入字段“name”值,如下所示:

 <input type="checkbox" name="section_<?php echo $section_id; ?>[<?php echo $step_numb; ?>][anso]" id="anso-check-<?php echo str_replace(".","-",$section_id)."-" .$step_numb; ?>" required="required">

 <input type="checkbox" name="section_<?php echo $section_id; ?>[<?php echo $step_numb; ?>][anst]" id="anst-check-<?php echo str_replace(".","-",$section_id)."-" .$step_numb; ?>" required="required">

<textarea onkeyup="textAreaAdjust(this)"  name="section_<?php echo $section_id; ?>[<?php echo $step_numb; ?>][com]"></textarea>

所以基本上它看起来像这样:

<input type="checkbox" name="section_5[1][anso]" id="anso-check-5-1" required="required">

<input type="checkbox" name="section_5[1][anst]" id="anst-check-5-1" required="required" checked="checked">

<textarea onkeyup="textAreaAdjust(this)" name="section_5[1][com]"></textarea>
  • anso,anst和com是不变的。

我试图抓住表单的POST:

$answer_sections        = array ();  // anso , anst , com

$section_count          = 1; // set to one to handle just thefirst section for testing.
$answer_section_index       = 0;
$section_index          = 5;

    while($section_count > 0){
        $answer_sections[$answer_section_index] = isset($_POST['section_'.$section_index]) ? $_POST['section_'.$section_index] : '';

        $section_index++;
        $answer_section_index++;
        $section_count--;
    }

我期待$ answer_sections [0]成为一个数组,它包含所有步骤及其答案。但是,如果我做sizeof($ answer_sections [0]),我得到一个:

PHP警告:sizeof():参数必须是实现Countable的数组或对象

这是我收到的许多错误中的第一个。

我使用的是多维数组错误吗?我想我明白,只要你有相同的输入名称,在这种情况下是“section_1”,那么它将被作为PHP中的数组进行处理。

转储是这样的:

    array(11) { 
            [1]=> array(2) { 
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } [2]=> array(2) { 
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } [3]=> array(2) {
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } [4]=> array(2) { 
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } [5]=> array(2) { 
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } [6]=> array(2) { 
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } [7]=> array(2) { 
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } [8]=> array(2) { 
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } [9]=> array(2) {
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } [10]=> array(2) { 
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } [11]=> array(2) { 
                  ["anso"]=> string(2) "on" ["com"]=> string(0) "" 
             } 
   } 
Array ( [1] => Array ( [anso] => on [com] => ) [2] => Array ( [anso] => on [com] => ) [3] => Array ( [anso] => on [com] => ) [4] => Array ( [anso] => on [com] => ) [5] => Array ( [anso] => on [com] => ) [6] => Array ( [anso] => on [com] => ) [7] => Array ( [anso] => on [com] => ) [8] => Array ( [anso] => on [com] => ) [9] => Array ( [anso] => on [com] => ) [10] => Array ( [anso] => on [com] => ) [11] => Array ( [anso] => on [com] => ) ) 

如果有人能提供帮助,我将不胜感激。

谢谢!

php html forms post multidimensional-array
1个回答
0
投票

我的方式是正确的。我有一些需要修复的语法错误。

© www.soinside.com 2019 - 2024. All rights reserved.