tags:

views:

135

answers:

3

Here's my situation: I'm building a small website where, when registring, you can select which days you're available for something.

Mondaymorning Mondayafternoon Tuesdaymorning Tuesdayafternoon etc...

All the way to sundayafternoon.

I'm formchecking this offcourse, in PHP, and I need to declare 14 variables and go over each one to determine wheter or not the checkbox was clicked.

Now I'm wondering, isn't there an easier way? Now, these are checkboxes, it's not like when uploading files you just add [] to the name atribute in HTML and they get uploaded as an array.

But that would be handy. Does anybody know how I can get something like this going?

+10  A: 

Actually, you can just add [] to the end of the names, just make sure that you set all their values differently.

<input type="checkbox" name="available[]" value="Mon AM" />
<input type="checkbox" name="available[]" value="Mon PM" />

Then just loop over the $_POST['available'] array in your form-processing code.

Chad Birch
Didn't know that. Thanks.
Vordreller
+1  A: 

Building on Chad's answer, here's the code you would use to go through the variables:

foreach($_POST['available'] as $a)
{
    //Do stuff with $a
}
Andrew
+1  A: 

If you are concerned about security, or performance, and you should be, maybe you should do this as well.

If (count($_POST['available']) > 14) {

    softErrorToClient('The data you entered could not be analysed. Please try again.');
    // log this error... someone's being naughty

}
alex