checkbox question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
will_steele
Forum Newbie
Posts: 3
Joined: Sat Sep 16, 2006 7:51 pm

checkbox question

Post 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.
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Code: Select all

$checkboxValue = ($_POST['myCheckbox'] == 'on') ? true : false;
$checkboxValue can now be evaluated as a boolean.
will_steele
Forum Newbie
Posts: 3
Joined: Sat Sep 16, 2006 7:51 pm

Post by will_steele »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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">
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
will_steele
Forum Newbie
Posts: 3
Joined: Sat Sep 16, 2006 7:51 pm

Post 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.
Post Reply