A
Anonymous
Guest
slecuyer said:i get the basics, and i know how to send a simple form through email. now im trying to include mailing of a survey.
i know know if i have this right, but here is how i THOUGHT checking for checkboxes would be:
if($cbox1 == 'checked'){
$cbox = "top";
if($cbox2 == 'checked'){
$cbox = "top and bottom";
if($cbox3 == 'checked){
$cbox = "top, bottom, and left";
if($cbox4 == 'checked'){
$cbox = "top, bottom, left, and right";
}
}
}
}
if($cbox2 == 'checked'){
// same as above avoiding $cbox1
}
you get the concept, but i was wondering if there was a simpler way to do this.
that, and is it ok that i use $cbox right away without declaring it, or is it suggestion to declare it to avoid browser errors?
thx in advance!
Use && (literally means "AND") to get the desired effect:
Code:
//If $cbox1 is check AND cbox 2 is unchecked AND cbox3 is unchecked AND cbox4 is unchecked
if($cbox1 == 'checked' && $cbox2 == 'unchecked' && $cbox3 == 'unchecked' && $cbox4 == 'unchecked'){
$cbox = "top";
}
if($cbox1 == 'checked' && $cbox2 == 'checked' && $cbox3 == 'unchecked' && $cbox4 == 'unchecked'){
$cbox = "top and bottom";
}
if($cbox1 == 'checked' && $cbox2 == 'checked' && $cbox3 == 'checked' && $cbox4 == 'unchecked'){
$cbox = "top, bottom and left";
}
if($cbox1 == 'checked' && $cbox2 == 'checked' && $cbox3 == 'checked' && $cbox4 == 'checked'){
$cbox = "top, bottom, left, and right";
}
}
However, you could also do this:
Code:
if($cbox1 == 'checked'){ $sections[] = "top"; }
if($cbox2 == 'checked'){ $sections[] = "bottom"; }
if($cbox3 == 'checked'){ $sections[] = "left"; }
if($cbox4 == 'checked'){ $sections[] = "right"; }
$startkey=key($sections);end($sections);$endkey=key($sections);reset($sections);
foreach($sections as $key=>$val){
if($key == $startkey){
$result .= $val;
} else {
if($key == $endkey){
$result .= "and ";
} else {
$result .= ", ";
}
$result .= $val;
}
}
What that does is it first checks each of the 4 $cbox'es for "checked". If it finds it, it adds the related section to the $section array. It then records the key of the first and last element in the array. The foreach loop then turns that array into a string type variable that can be output onto the screen.
That way, it wont matter which check boxes they check, it will always output the correct sections. So if cbox 1, 3, and 4 are "checked" then it would output "top, left, and right". If only cbox1 and 4 are checked: "top and left".
It may be above your caliber but that's the simplest way to do what I think you're trying to do.
Oh and to answer your question about if you can use $cbox before declaring it, yes you can. PHP will set any undeclared variables to NULL. Heres an example:
Code:
<?php
var_dump($variable);//As is
var_dump((bool)$variable);//Force to boolean
var_dump((string)$variable);//Force to string
var_dump((int)$variable);//Force to Integer
?>
That will produce a NULL, bool(false), string(0)"", and int(0). Which means $variable is set to all that even though it was never actually declared.