How do you detect status of a check box?

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
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

How do you detect status of a check box?

Post by mjseaden »

Dear All

In PHP, how do I detect the status (on/off) of a checkbox in PHP through its $_POST variable - how does the POST variable change/differ depending on a ON or OFF state?

Many thanks

Mark
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

I think its

Code: Select all

//its either 1 or 'checked' im not sure i dont remember
if($_POST['checkbox'] == 1)
{
echo 'its checked';
}
else
{
echo 'Nope';
}
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It gets given the value you assign it in the form, eg value="1", or value="yes" etc.. it's gets assigned 1 for a 'checked' value by default if no value is given.
An unchecked checkbox just won't be posted so it won't even appear in the $_POST array.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Code: Select all

<?php
echo '<pre>';
  print_r ($_POST);
echo '</pre>';
?>
That will give you a list of all the $_POST variables sent to the page, and thier values.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Thanks very much, I'll give it a go.
Post Reply