Jump to content


Photo

unchecked post value for checkbox


  • Please log in to reply
6 replies to this topic

#1 crimsonmoon

crimsonmoon
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 14 June 2006 - 09:29 PM

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?

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 June 2006 - 09:40 PM

[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.
Legend has it that reading the manual never killed anyone.
My site

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 14 June 2006 - 10:00 PM

You can initialize your check boxes to a known value like this:
<input type="hidden" name="box2" value="no">
<input type="checkbox" name="box2" value="yes">
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

#4 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 14 June 2006 - 11:12 PM

[!--quoteo(post=383999:date=Jun 14 2006, 03:00 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 14 2006, 03:00 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You can initialize your check boxes to a known value like this:
<input type="hidden" name="box2" value="no">
<input type="checkbox" name="box2" value="yes">
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.

Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )

#5 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 June 2006 - 11:15 PM

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.
Legend has it that reading the manual never killed anyone.
My site

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 15 June 2006 - 12:25 AM

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:
<input type="hidden" name="box2[0]" value="no">
<input type="checkbox" name="box2[1]" value="yes">
Then your check would be:
<?php
$box2 = (count($_POST['box2']) == 2)?$_POST['box2'][1]:$_POST['box2'][0];
?>
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

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 15 June 2006 - 04:22 AM

why are you overcomplicating it? like AndyB said, just do:

if ($_POST['box'] && !$_POST['box2']) { ... }
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users