Checkbox Validation: Undefinded error

Error Message: Notice: Undefined variable: membership in line 100

Code:

if (isset($_POST['membership'])) {
  $membership = implode(', ', $_POST['membership']);
} else {
   $error['membership'] = 'Please select all that apply';
   } 

100)	$message .= "Annual Membership/President\\'s Circle: $membership\\r\
\\r\
";

<fieldset>
<label for="membership">Please select all that apply:</label>
<input name="membership[]" type="checkbox" id="annualmembership" value="Annual Membership" <?php
			$OK = isset($_POST['membership']) ? true : false;
			if ($OK && isset($error) && in_array('Annual Membership', $_POST['membership'])) { ?> checked="CHECKED" <?php } ?>	/>
<label for="annualmembership">Annual Membership</label>

<input type="checkbox" name="membership[]" value="President's Circle" id="presidentcircle" <?php
			if ($OK && isset($error) && in_array('President\\'s Circle', $_POST['membership'])) { ?> <?php } ?> />
<label for="presidentcircle">President's Circle (optional)</label>
<br />
<?php if (isset($error['membership'])) { ?>
<span class="warning"><?php echo $error['membership']; ?></span>
<?php } ?>
</fieldset>

OK. My first problem is that when my application first loads, the first checkbox selection is not checked–I thought I had that figured out.

My second problem is that if someone dechecks the selections (which shouldn’t happen), the correct error is applied, but then I get the “Notice: Undefined variable: membership in line 100” obviously caused by a checkbox option is not set.

How do I set the default checkbox? and how do I prefent the ‘Undefined’ error if someone decides to decheck the selections?

you can do simple

$membership="";

at the top of your code.

For the “checked” problem, try starting off with it checked, and if $POST isset and false, then make it not checked.

For the notice error, simply define it as empty before the $POST if as Shrapnel_NS showed.

OK.

#The first set of changes, I did this:

$membership = $_POST['membership'];
if (empty($membership)) {
	$error['membership'] = 'Please select all that apply';
	}

I loaded my app, still no default checked, which I thought this took care of it:

<input name="membership[]" type="checkbox" id="annualmembership" value="Annual Membership" <?php
			$OK = isset($_POST['membership']) ? true : false;
			if ($OK && isset($error) && in_array('Annual Membership', $_POST['membership'])) { ?> checked="CHECKED" <?php } ?>	/>

It shows checked=“CHECKED” which means that it is set to be the default checked option, but it doesn’t show that in the browser.

#2 If I submit the form with the first option checked, it will return the same value (YEAH!); but if say they checked also the second option with it and submit the form, the checkmark disappears on that one.

#3 If both checkmarks are not checked, I get the correct error message:
“Please select all that apply”, but still get the ‘Notice: Undefined index: membership in line 76’ which is this:

$membership = $_POST['membership'];

So then I tried this:

$membership = $_POST['membership'];
if (empty($membership)) {
	$error['membership'] = 'Please select all that apply';
	} else {
	$membership = implode(', ', $_POST['membership']);
	}

Did the same exact tests. The only error I received was “Notice: Undefined index: membership in line 76” when both options were dechecked; when I select the 2nd checkbox option, send the form, it doesn’t retain the value, no error message.

So then I tried this:

$membership = "";
if (empty($membership)) {
	$error['membership'] = 'Please select all that apply';
	} else {
	$membership = implode(', ', $_POST['membership']);
	}

Test again. With blank checkboxes, no error message, except the one that says ‘Please select all that apply’, which is good. BAD: I select the first checkbox option and I get the same error message ‘Please select all that apply’. Check both boxes, same error message.

Where am I going wrong?

So is there no solution to the problem?

$membership = “”;
if (empty($membership)) {

Use empty($_POST[‘membership’]) otherwise you are setting $membership to empty and then checking it’s empty :stuck_out_tongue:

Also you’re missing an echo 'checked… in the original form code.

Thank you, hash! This worked out very nicely. I was up till 1:30am trying to figure this out!

You guys are awesome!