Page 1 of 1

How do you detect status of a check box?

Posted: Sun Mar 28, 2004 1:53 pm
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

Posted: Sun Mar 28, 2004 2:53 pm
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';
}

Posted: Sun Mar 28, 2004 2:55 pm
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.

Posted: Sun Mar 28, 2004 2:56 pm
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.

Posted: Sun Mar 28, 2004 2:58 pm
by mjseaden
Thanks very much, I'll give it a go.