Jump to content

Archived

This topic is now archived and is closed to further replies.

crimsonmoon

unchecked post value for checkbox

Recommended Posts

Ok i have a php scrit where a checkbox is named box.

It's been working where if $_POST[box] == "yes" then do something.


well I want to now have it where if $_POST[box] == "yes" && $_POST[box2] == "no" then do something.

I don't think the no is working because it's not hitting it.

Any suggestions? Or what the right option for an unchecked box is?

Share this post


Link to post
Share on other sites
[a href=\"http://www.onlamp.com/pub/a/php/2003/03/13/php_foundations.html\" target=\"_blank\"]http://www.onlamp.com/pub/a/php/2003/03/13...oundations.html[/a]

The 'right' value for an unchecked checkbox is that it has no value.

Share this post


Link to post
Share on other sites
You can initialize your check boxes to a known value like this:
[code]
<input type="hidden" name="box2" value="no">
<input type="checkbox" name="box2" value="yes">
[/code]
When this code is processed by the browser, if the box isn't checked your script will see the "no" value, if it is checked, the "yes" value will be passed.

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=383999:date=Jun 14 2006, 03:00 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 14 2006, 03:00 PM) [snapback]383999[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You can initialize your check boxes to a known value like this:
[code]
<input type="hidden" name="box2" value="no">
<input type="checkbox" name="box2" value="yes">
[/code]
When this code is processed by the browser, if the box isn't checked your script will see the "no" value, if it is checked, the "yes" value will be passed.

Ken
[/quote]

That's a great solution to the checkbox problem!!
The way I always did it was to set all your checkbox vars to 'no' and then only set them to 'yes' if you found your checkbox in the $_POST array.

Share this post


Link to post
Share on other sites
Cautionary quote about the method Ken mentions from the link I gave:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Unfortunately, the drawback to this solution is a complete reliance on the browser to always send the hidden element before the checkbox element. Although it is logical to assume that elements will be sent in the same order as they are presented in the HTML document, there is no guarantee that this situation will be the case.[/quote]

The article offers a solution to that.

Share this post


Link to post
Share on other sites
Interesting article. I can see the author's point, although I have never hit that situation (yet).

Here's another solution I just thought up (It's probably not original, but ...) [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Instead of having the hidden field with the same same, turn the name into an array. assign the "no" value explicitly to the 0th element and the "yes" value to the 1st element:
[code]
<input type="hidden" name="box2[0]" value="no">
<input type="checkbox" name="box2[1]" value="yes">
[/code]
Then your check would be:
[code]<?php
$box2 = (count($_POST['box2']) == 2)?$_POST['box2'][1]:$_POST['box2'][0];
?>[/code]
This doesn't reply on the order that the information is presented, just that an unchecked box isn't presented. If the box is not checked the box2 array will contain 1 element, if it is checked -- 2 elements.

Ken

Share this post


Link to post
Share on other sites
why are you overcomplicating it? like AndyB said, just do:

if ($_POST['box'] && !$_POST['box2']) { ... }

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.