I’ve tried for over a week to validate a checkbox group. I did numerous searches and got bits and pieces from this site that helped, but there was nothing that put it all together. So in an effort to save others the time and frustration I went through, here is what worked for me.
I used sessions in a multiple page form that submits to the next page if all required fields are filled in, but stays on the same page and redisplays the form with everything still filled in and error messages at the top for what was missing or wrongly inputted.
Here is the code for the form action:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="enroll5" id="enroll5">
In the form itself, here is how I coded the checkboxes:
<p><span class="greenBold">*</span> What services are you interested in? (choose all that apply)</p>
<p class="indent"><input name="services2[visits]" type="checkbox" id="visits" value="Home Visits"<?php if (isset($_SESSION['services2']) && in_array('Home Visits', $_SESSION['services2'])) echo ' checked="checked"'; ?> /> Home Visits</p>
<p class="indent"><input name="services2[meetings]" type="checkbox" id="meetings" value="Group Meetings"<?php if (isset($_SESSION['services2']) && in_array('Group Meetings', $_SESSION['services2'])) echo ' checked="checked"'; ?> /> Group Meetings</p>
<p class="indent"><input name="services2[playgroups]" type="checkbox" id="playgroups" value="Drop-in Playgroups"<?php if (isset($_SESSION['services2']) && in_array('Drop-in Playgroups', $_SESSION['services2'])) echo ' checked="checked"'; ?> /> Drop-in Playgroups</p>
<p class="indent"><input name="services2[screenings]" type="checkbox" id="screenings" value="Screenings"<?php if (isset($_SESSION['services2']) && in_array('Screenings', $_SESSION['services2'])) echo ' checked="checked"'; ?> /> Screenings</p>
At the top of the page is the call to start sessions and register the variables in case there are error messages:
<?php
// pass on variables from page to page
session_start();
header("Cache-control: private"); //IE 6 Fix
// register new session variables if error messages
...
$_SESSION['services2'] = $_POST['services2'];
...
?>
I use a class validation script …
//Validate_fields Class - verion 1.35
//Easy to use form field validation
//Copyright (c) 2004 - 2006, Olaf Lederer
include(" ... link to validation class file ...");
if (isset($_POST['submit'])) {
$example = new Validate_fields;
$example->check_4html = true;
...
$example->add_check_box("What services are you interested in?", "services2", "checkbox", "");
if ($example->validation())
{
Header("Location: *****.php");
}
else
{
$error = $example->create_msg(); // show error messages
}
}
On the processing page I have the session start again … it’s on every page.
In case the page submitted without errors I register the variables again using an if statement.
// register new session variables
if (!$_SESSION['services2']) // in case there's already data - ignore
{$_SESSION['services2'] = $_POST['services2'];}
And finally to make sure it works correctly on my test processing page I have it print the results
...
foreach($_SESSION['services2'] as $s2) echo $s2.'<br \\>';
...
I want to thank the administrators of this site and the people who really help newbies to PHP and people like me who have worked with it some but still have a lot to learn. Hope this helps someone else.