tags:

views:

320

answers:

3
<form method="POST">

    <input type="checkbox" id="hrm" name="hrm" />

</form>

I mean when the form is posted.

A: 

This how:

<?PHP
if($_POST['hrm']=='ok') echo 'checked';
else echo 'not';
?>

or:

<?PHP
if(isset($_POST['hrm'])) echo 'checked';
else echo 'not';
?>

But first you have to give value for it:

<input type="checkbox" id="hrm" name="hrm" value='ok' />
lfx
Is value neccesary?I saw from firebug that without value it'll post something like "hrm: On".
Shore
Watch your typos. Or `else`
random
I'd suggest to add a value since you're otherwise depending on the user's browser to add that "On" value. I don't know if you can expect every browser to do that for you. And if a user comes along without he's screwed.
Jörg
+2  A: 
<input type="checkbox" id="hrm" name="hrm" value="yes" />


<?php

if ( isset( $_POST['hrm']) && $_POST['hrm'] === 'Yes' ) {
}

?>
meder
Seems isset( $_POST['hrm']) is enough,since when not checked,won't post an antry with key "hrm" at all,right?
Shore
isset() should be enough, I just like being thorough and that would explain the extra check for the value being exactly what I want.
meder
+3  A: 

$_GET['hrm'] or $_POST['hrm'] (depending on your form's method attribute) will be set to 'On' if it is checked, or will not be set at all if it's unchecked. In essence, you can just check using isset($_GET['hrm']) (or _POST if that's the case) - if isset() returns true, then it was checked.

Amber
So,it's the standard,right?
Shore
Yes, that's the standard.
Amber