PHP Post Multi Checkbox Sets Array

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

我有一个包含多个复选框集的表单,其中输入的名称是动态创建的(name =“'。$ name。'[]”)。 HTML结果如下所示:

<tr>
<td><input type="checkbox" name="1[]" value="Jan"/></td>
<td><input type="checkbox" name="1[]" value="Feb"/></td>
</tr>
<tr>
<td><input type="checkbox" name="2[]" value="Jul"/></td>
<td><input type="checkbox" name="2[]" value="Sep"/></td>
<td><input type="checkbox" name="2[]" value="Dec"/></td>
</tr>
<tr>
<td><input type="checkbox" name="3[]" value="May"/></td>
<td><input type="checkbox" name="3[]" value="Aug"/></td>
</tr>

提交后,我知道它是一个多维数组,但是如何使用foreach语句获取输入名称的值?目前,我已经尝试过了,但是它没有给我输入名称的值。

foreach ($_POST as $task){
    foreach ($task as $key => $month){
        echo '<p>Task ID is: '. $task .' and the month check is '. $month .'';
    }       
}

哪个返回(如果全部选中):

Task ID is: Array and the month check is Jan
Task ID is: Array and the month check is Feb
Task ID is: Array and the month check is Jul
Task ID is: Array and the month check is Sep
Task ID is: Array and the month check is Dec
Task ID is: Array and the month check is May
Task ID is: Array and the month check is Aug

我需要输入名称作为任务ID:

Task ID is: 1 and the month check is Jan
Task ID is: 1 and the month check is Feb
Task ID is: 2 and the month check is Jul
Task ID is: 2 and the month check is Sep
Task ID is: 2 and the month check is Dec
Task ID is: 3 and the month check is May
Task ID is: 3 and the month check is Aug

非常感谢您提供任何帮助。

php html arrays post checkbox
1个回答
0
投票

您的$ _POST将具有表单帖子名称作为键。因此,您将必须从第一个foreach循环中获取密钥,如下所示:

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