Jump to content

unchecked post value for checkbox


crimsonmoon

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?
Link to comment
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.
Link to comment
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
Link to comment
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.
Link to comment
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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.