如何回显复选框组


how to echo checkbox group?

谁能帮帮我?我不能用多个选择来回应我的复选框组。每次我回显复选框组时,唯一显示的是我选中的最后一个框。

这是我的代码

<?php
$submit = $_POST['submit'];
$incharge = $_POST['incharge'];
if ($submit)
{
   echo $incharge;
}
?>
<table width="500" height="69">
      <tr>
        <td width="73"><label>
          <input type="checkbox" name="incharge" value="1" id="responsible_0" />
          MNFA</label></td>
          <td width="72"><label>
          <input type="checkbox" name="incharge" value="2" id="responsible_1" />
          HJB</label></td>
          <td width="70"><label>
          <input type="checkbox" name="incharge" value="3" id="responsible_2" />
          JBG</label></td>
          <td width="75"><label>
          <input type="checkbox" name="incharge" value="4" id="responsible_3" />
          MSG</label></td>
          <td width="275"><label>
          <input type="checkbox" name="incharge" value="5" id="responsible_4" />
          MGR</label></td>
      </tr>
      <tr>
      <td height="33"><label>
          <input type="checkbox" name="incharge" value="6" id="responsible_5" />
          AAP</label></td>
          <td><label>
          <input type="checkbox" name="incharge" value="7" id="responsible_6" />
          EPM</label></td>
          <td><label>
          <input type="checkbox" name="incharge" value="8" id="responsible_7" />
          SGA</label></td>
          <td><label>
          <input type="checkbox" name="incharge" value="9" id="responsible_8" />
          JLL</label></td>
          <td><label>
          <input type="checkbox" name="incharge" value="10" id="responsible_9" />
          AFM</label></td> 
      </tr>
    </table>

将name属性改为:

name="incharge[]"

将生成一个数组$incharge。

注意,你不能只是回显该值;您需要"print_r"或循环遍历它。

您需要更改"input"元素的"name"属性,通过在末尾添加方括号[]来指示它是一个数组。$_POST['incharge']将是一个数组而不是字符串。

<input type="checkbox" name="incharge[]" value="1" id="responsible_0" />

只发送最后一个值的原因是所有的复选框都有相同的名称,因此会反复重命名它们。您需要的是将所有复选框分配给一个数组,如下所示:

name="incharge"改为name="incharge[]"

你会想要迭代它:

if ($submit)
{
    // PHP throws a fit if we try to loop a non-array
    if(is_array($incharge))
    {
        foreach($incharge as $val)
        {
            echo $val . '<br />';
        }
    }
}
if(isset($_post['calculations'])
{
    $member = $_POST['member'];//get the total values in an array
    if(is_array($member))// confirm $member is an array
    {
        foreach($member as $names)
        {
            echo $names ."<br/>";//take the values 
        }
    }

<input type="checkbox" name="member[]" value="value1">