开发者

PHP loop through checkbox array to show values

开发者 https://www.devze.com 2023-02-13 15:58 出处:网络
How do I loop through a checkbox array so it shows each value? Original code: <form class=\"form\" method=\"POST\" action=\"index.php?id=22\">

How do I loop through a checkbox array so it shows each value?

Original code:

  <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="CheckboxGr开发者_如何学JAVAoup2" 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>


put name="CheckboxGroup2[]"

and get the data in array form


In the spirit of @diEcho's post, your form should look like

<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

On the backend, the $_POST['CBgroup1'] field will be an array instead of a single value. You'll want to loop through that array to see which ones are checked.


Solved with:

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


You can use

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.";

I wanted this in reply to eykanal, but I don't think I have enough reputation. (Please tell me if I missed something obvious, this is my first post ever =)

0

精彩评论

暂无评论...
验证码 换一张
取 消