Page 1 of 1
checkbox question
Posted: Sat Sep 16, 2006 7:55 pm
by will_steele
I want to run a small code block based on whether a checkbox is 'checked' or not. I tried gathering the global using post and a submit for checked as both 'checked', '' and true/false, as well as 1 or 0. Is there another way to approach validating checkbox values? I thought about running some eval() tests on the value to see what the return is (string, bool or int) but didn't quite know how to test it.
Posted: Sat Sep 16, 2006 8:00 pm
by gkwhitworth
I just learned about these it is actually on or off:
Code: Select all
if ($variable_name == "on") { echo "You have selected such and such";}
if ($variable_name == "off") { echo "You haven't selected such and such";}
Hope that helps
--
Greg
Posted: Sat Sep 16, 2006 9:36 pm
by aaronhall
Code: Select all
$checkboxValue = ($_POST['myCheckbox'] == 'on') ? true : false;
$checkboxValue can now be evaluated as a boolean.
Posted: Sat Sep 16, 2006 9:47 pm
by will_steele
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I'm still learning and working on basics like this, so pardon if this is something obvious I'm missing. Please see code below:
Code: Select all
<form action="test_form.php" method="post" name="test" target="_self">
<input name="test_button" type="checkbox" value="" />
<?php $checkboxValue = ($_POST[$test_button] == 'on') ? true : false;
if($checkboxValue)
{
echo 'true';
} else {
echo 'false';
}
?>
<input name="Submit" type="submit" />
</form>
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Sat Sep 16, 2006 9:53 pm
by aaronhall
What is it that you're trying to do?
HTML submits checkbox input values as either "on" or "off" (PHP sees them as strings).
If you want to make a checkbox checked by default:
Code: Select all
<input type="checkbox" name="myCheckbox" checked="checked">
Posted: Sat Sep 16, 2006 10:02 pm
by aaronhall
I'll also add that if you are trying to set a checkbox's state to that the previously submitted form (the form would, of course, have to submit to itself):
Code: Select all
<input type="checkbox" name="myCheckbox" <?=($_POST['myCheckbox']=='on')?'checked="checked"':'';?> /> My Checkbox
The code's readability is low, but its much better than spanning the same logic over multiple lines, especially if you have many checkboxes.
Posted: Sun Sep 17, 2006 3:01 am
by volka
aaronhall wrote:HTML submits checkbox input values as either "on" or "off" (PHP sees them as strings).
What client submits
off if the checkbox is not selected?
Posted: Sun Sep 17, 2006 2:06 pm
by will_steele
aaronhall wrote:What is it that you're trying to do?
HTML submits checkbox input values as either "on" or "off" (PHP sees them as strings).
If you want to make a checkbox checked by default:
Code: Select all
<input type="checkbox" name="myCheckbox" checked="checked">
All I'm after is creating a page with a section of checkbox options, that, when submitted, returns segments of code based on what checkboxes are enabled...or turned on as code would have it.