Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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
}

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top