Skimming through this I didn't see anything that really answered the question so I'll give it a shot. Keep in mind I'm a programmer and a motto around here is "friends don't let friends use javascript." I'm not a fan of interpreted languages such as PHP because its so loosely coded but on the web you don't have much of a choice so my solution would be to create a function for example:
<form action="<?php $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<input type="checkbox" name="box1" VALUE="1" />
</form>
<?php
$checkbox = $_POST['box1'];
function isChecked($check_box_name) {
if (!empty($check_box_name)) {
return true;
} else {
return false;
}
}
if (isChecked($checkbox)) {
echo 'Checked';
} else {
echo 'Not checked';
}
?>
- <form action="<?php $_SERVER['SCRIPT_NAME']; ?>" method="POST">
- <input type="checkbox" name="box1" VALUE="1" />
- </form>
-
- <?php
-
- $checkbox = $_POST['box1'];
- function isChecked($check_box_name) {
- if (!empty($check_box_name)) {
- return true;
- } else {
- return false;
- }
- }
-
- if (isChecked($checkbox)) {
- echo 'Checked';
- } else {
- echo 'Not checked';
- }
- ?>
-
One reason I don't like interpreted languages is because you cant define a variable's data type. I don't know how PHP returns booleans(my guess is false = 0, true = 1) or even how it would recognize one so I'd much rather create a function to act in a way that I expect. I haven't tested this code but I don't see why it wouldn't work.
If you want further explanation on the code, the way I understand check boxes in html is they only return a value if they're checked. So if we use an if statement to check if the value is not empty it must be checked therefore it will return true otherwise return false and for one checkbox the parameter for the function isChecked() is unnecessary, I included the parameter because you may have multiple check boxes so you can use the same function for all check boxes by storing the data returned by each check box in the form in its own variable and inserting that variable you want to check so isChecked($checkbox) tells the function to return the value for $checkbox. Also instead of returning true or false you can return any value you'd like of any data type (if there's a way to define data types in this sloppy language). If you have multiple check boxes you can define what to do if different combinations of them are selected by expanding the if statement in the function.
Finally, just because I don't like this particular language don't underestimate it, PHP is a very powerful language that can do a lot of different things and my opinion doesn't change that.