PHP循环通过复选框数组来显示值

时间:2011-02-25 14:50:18

标签: php forms phpmailer

如何循环复选框数组以显示每个值?

原始代码:

  <form class="form" method="POST" action="index.php?id=22">
<? if (!$_POST['step']) { ?>
<input type="hidden" name="step" value="1" />
        <tr>
      <td width="300" valign="top"><label style="margin-right: 25px;">
          <input style="width: 25px;" type="checkbox" name="CheckboxGroup2" value="Pharmaceuticals" id="member3_pharma" />Pharmaceuticals</label>
        &nbsp;&nbsp;&nbsp;</td>
      <td width="300" valign="top"><label style="margin-right: 25px;">
          <input style="width: 25px;" type="checkbox" name="CheckboxGroup2" value="Medical Devices" id="member3_devices" />
          Medical Devices</label>
        &nbsp;&nbsp;&nbsp;</td>
      <td width="300" valign="top"><label style="margin-right: 25px;">
          <input style="width: 25px;" type="checkbox" name="CheckboxGroup2" value="Legal" id="member3_legal" />
          Legal</label>
        &nbsp;&nbsp;&nbsp;</td>
    </tr>
    <button class="blue medium awesome awesomeforward" style="margin: 10px; float: right;" type="submit" name="submit">Next step</button>
<? } else if ($_POST['step'] == 3) {

foreach($_POST as $name => $value) {
    if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; }
}

?>
        <tr>
      <td><label for="member4_total">Total no. employees (Welsh Site)</label></td>
      <td>&nbsp;</td>
      <td><input type="text" name="member4_total" id="member4_total" /></td>
    </tr>
    <tr>
      <td><label for="member4_turnover">Turnover (from Welsh site)</label></td>
      <td>&nbsp;</td>
      <td><input type="text" name="member4_turnover" id="member4_turnover" /></td>
    </tr>
    <? } else if ($_POST['step'] == 4) { //do posting and send email here! 

$to = "test@test.com";
$subject = "MediWales member application";

$CheckboxGroup2_field = $_POST['CheckboxGroup2'];

$body = "$CheckboxGroup2_field";


mail($to, $subject, $body);

echo "Thank you for your account application, we will contact you shortly.";

} ?>
  </form>

4 个答案:

答案 0 :(得分:4)

name="CheckboxGroup2[]"

以数组形式获取数据

答案 1 :(得分:4)

根据@ diEcho的帖子,你的表格看起来应该是

<input type='checkbox' name='CBgroup1[]' value='1'> One
<input type='checkbox' name='CBgroup1[]' value='2'> Two
<input type='checkbox' name='CBgroup1[]' value='3'> Three
<input type='checkbox' name='CBgroup1[]' value='4'> Four

<input type='checkbox' name='CBgroup2[]' value='1'> One
<input type='checkbox' name='CBgroup2[]' value='2'> Two
<input type='checkbox' name='CBgroup2[]' value='3'> Three
<input type='checkbox' name='CBgroup2[]' value='4'> Four

在后端,$_POST['CBgroup1']字段将是一个数组而不是单个值。您需要遍历该数组以查看检查了哪些数据。

答案 2 :(得分:2)

解决:

foreach ($_SESSION['CheckboxGroup1'] as $val) {
$checkbox1results .= $val.",\n";
}

答案 3 :(得分:0)

您可以使用

if(isset($_POST['CBgroup1']))
#to ensure that $_POST[yadda] exists, otherwise you get a happy error
    if(in_array("4",$_POST['CBgroup1']))
    #searches your array for "4", if you like this make sure each value checkbox is unique
         echo "The fourth checkbox is checked.";

我想回复eykanal,但我认为我没有足够的声誉。 (请告诉我,如果我错过了一些明显的东西,这是我的第一篇文章=)