crimsonmoon Posted June 14, 2006 Share Posted June 14, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/12021-unchecked-post-value-for-checkbox/ Share on other sites More sharing options...
AndyB Posted June 14, 2006 Share Posted June 14, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/12021-unchecked-post-value-for-checkbox/#findComment-45723 Share on other sites More sharing options...
kenrbnsn Posted June 14, 2006 Share Posted June 14, 2006 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 Link to comment https://forums.phpfreaks.com/topic/12021-unchecked-post-value-for-checkbox/#findComment-45727 Share on other sites More sharing options...
dptr1988 Posted June 14, 2006 Share Posted June 14, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/12021-unchecked-post-value-for-checkbox/#findComment-45741 Share on other sites More sharing options...
AndyB Posted June 14, 2006 Share Posted June 14, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/12021-unchecked-post-value-for-checkbox/#findComment-45743 Share on other sites More sharing options...
kenrbnsn Posted June 15, 2006 Share Posted June 15, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/12021-unchecked-post-value-for-checkbox/#findComment-45766 Share on other sites More sharing options...
.josh Posted June 15, 2006 Share Posted June 15, 2006 why are you overcomplicating it? like AndyB said, just do: if ($_POST['box'] && !$_POST['box2']) { ... } Quote Link to comment https://forums.phpfreaks.com/topic/12021-unchecked-post-value-for-checkbox/#findComment-45797 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.