0

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="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 = "[email protected]";
$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 Answers 4

4

put name="CheckboxGroup2[]"

and get the data in array form

Sign up to request clarification or add additional context in comments.

6 Comments

I'm not quite sure how to do that. I've got a multi step form that's doing this at the top aswell - <? if (!$_POST['step']) { ?>
@Rob: Maybe you're thinking about it too hard because it's really that simple. Rename 'CheckboxGroup2' to 'CheckboxGroup2[]', and that should be it. PHP automatically turns such variables into arrays. - after that you just need to know how to loop through an array. :)
Which I don't know how to do :/
Ok, I'm getting somewhere now, it's just saying array in the email so just need to know how to loop through the checkboxes.
$_POST['CheckboxGroup2'] holds the array. So do this: foreach($_POST['CheckboxGroup2'] as $key=>$value) {echo $key."= ".$value."<br/>";} this will output the values of all the checked checkboxes
|
4

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.

2 Comments

Thanks but as you can see from the comments on the other answer, I don't know how to loop through the array to show the results. I kinda need some hand holding on this!
@Rob - You want to use foreach($_POST['CBgroup1'] as $key=>$value). Every value which was checked will be in the array, and those that weren't checked won't be present. Play around with it and you'll see how it works.
2

Solved with:

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

1 Comment

If you want to comma-separate them, you could also use implode(', ', $_SESSION['CheckboxGroup1']). You wouldn't even need to use the foreach().
0

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 =)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.