【发布时间】:2019-07-27 04:07:45
【问题描述】:
我有一个包含几个部分的表单,每个部分都有几个步骤。每个步骤至少有一个复选框,最多两个,并且可以有一个 cmets 文本区域。
我正在从数据库中加载整个表单(问题),并且我创建了一个循环来执行此操作。我还在通过循环时命名输入字段“名称”值,如下所示:
<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] => ) )
如果有人能提供帮助,我将不胜感激。
谢谢!
【问题讨论】:
-
你能在 $_POST 上使用
var_dump来检查结构是否正确吗? -
@MacBooc 我得到:字符串(0)“”
-
那你的form不对,不是php而是HTML部分
-
我忘了我的部分是从 #5 开始的,所以将 #section_index 更新为 5。
-
用 var_dump 更新
标签: php html forms post multidimensional-array