problem with checkbox

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
acheoacheo
Forum Newbie
Posts: 2
Joined: Wed Jan 30, 2008 4:29 pm

problem with checkbox

Post by acheoacheo »

If that checkbox remains unchecked I can't retreive the value "off" in the $_POST variable when the form is submitted.

However if it is checked then I can retreive it...what's the problem??????????

Code: Select all

<input type="checkbox" name="Agreement" id="Debug" />

Code: Select all

if ($_POST['Agreement'] == "off"){
        $error_message["agreement"] = "You haven't read the Terms of use.";     
        $isOk = 0;
    }
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: problem with checkbox

Post by Christopher »

acheoacheo wrote:If that checkbox remains unchecked I can't retreive the value "off" in the $_POST variable when the form is submitted.
Absolutely correct!
acheoacheo wrote:However if it is checked then I can retreive it...what's the problem??????????

Code: Select all

if (! isset($_POST['Agreement'])){
        $error_message["agreement"] = "You haven't read the Terms of use.";     
        $isOk = 0;
    }
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: problem with checkbox

Post by RobertGonzalez »

empty() will be of great value to you here.
acheoacheo
Forum Newbie
Posts: 2
Joined: Wed Jan 30, 2008 4:29 pm

Re: problem with checkbox

Post by acheoacheo »

thank you!
Festy
Forum Commoner
Posts: 28
Joined: Wed Jan 30, 2008 10:01 pm

Re: problem with checkbox

Post by Festy »

If the checkbox is checked, you get the value 'on' but if it's unchecked you don't get any value at all (not even off) or even the refrence of your checkbox object for that matter.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: problem with checkbox

Post by RobertGonzalez »

That is why you check empty() on it. If it is empty then it is not checked, otherwise, it is.
Post Reply