Form Check box validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Form Check box validation

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Post 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 ""
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hint: Using a loop control would greatly simplify the check.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Post 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 ...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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"
Post Reply