Page 1 of 1

Form Check box validation

Posted: Fri Mar 02, 2007 9:43 pm
by me!
I need ideas on how to validate a form...

A registration form has 9 weeks of camp a user can sign up for and each week has 6 registration options.

Looks like this:

Week of July 1-5
[ ] - Full day Monday - Friday
[ ] - Full day Monday - Wednesday - Friday
[ ] - Full day Tuesday & Thursday
[ ] - Half day Monday - Friday
[ ] - Half day Monday - Wednesday - Friday
[ ] - Half day Tuesday & Thursday

Since there are nine weeks and six options each that is 54 check boxes. Each box is named "option_X" (option_1 - option_54)

The validation needs to:
* require min. of one selection (they need to select at least one week to attend)
* not allow multiple selections from the same week (in the example option_1 - option_6)
* I would like to run validation for each "week" (group of "options_X") for error processing reasons...

I considered using radio buttons, one set per week, but then a user can't un-select a week once they have selected an option. :(

Any Suggestions?

Posted: Fri Mar 02, 2007 10:05 pm
by feyd
Radio buttons wouldn't lend themselves to multiple selections for a given week either. .. but why is it allowed to have multiple selections for a given week? If there's no solid reason, radio buttons are the direction to head. Each week's group of choices would be named exactly the same and you could include a "not attending" type of choice that is the default state.

Posted: Fri Mar 02, 2007 10:25 pm
by me!
I do not want them to be able to select more than one option per week.

As for the "not attending" option for the radio buttons I was picturing that exact thing in my mind as I just asked about it :lol: That would probley be the best, since they could not make an incorrect selection.

So now the question is how do I make sure they select at least one week to attend...
Any better way than this:

Code: Select all

if (empty($week_1))
	{
	 if (empty($week_2))
		{
		if (empty($week_3))
			{
			if (empty($week_4))
				{
				if (empty($week_5))
					{
					// You get the idea...
					echo '******** ERROR *********';
					} 
				} 
			} 
		} 
	} 
I used empty() and would set the default value to ""

Posted: Fri Mar 02, 2007 10:48 pm
by feyd
hint: Using a loop control would greatly simplify the check.

Posted: Fri Mar 02, 2007 11:00 pm
by me!
I am not very good with loops yet, can you please give an example of one that does the exact same thing as the if statements?

Posted: Fri Mar 02, 2007 11:09 pm
by feyd
It would actually involve the original data in $_POST for example. You would probably use foreach() or possibly a "for" loop.

I'd like you to attempt some stuff before I supply more.

Posted: Fri Mar 02, 2007 11:45 pm
by me!
I looked at foreach() again but dot see how to use it, (need a hint)

As for the $_POST info,

Code: Select all

$option_1 = $_POST ['option_1'];  // week 1
$option_2 = $_POST ['option_2'];
$option_3 = $_POST ['option_3'];
$option_4 = $_POST ['option_4'];
$option_5 = $_POST ['option_5'];
$option_6 = $_POST ['option_6'];
I am using $ anyway ...

Posted: Fri Mar 02, 2007 11:54 pm
by feyd
Okay, here's another direction: selecting a good field name.

What happens to $_POST if you named each group "option[1]", "option[2]", etc?

Posted: Sat Mar 03, 2007 8:44 am
by Bill H
I considered using radio buttons, one set per week, but then a user can't un-select a week once they have selected an option.
They could if one of the radio buttons for each week was "omit this week"