0
<form method="POST">

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

</form>

I mean when the form is posted.

1
  • For future reference: var_dump($_POST); :-) Commented Sep 10, 2009 at 8:11

3 Answers 3

4

$_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.

Sign up to request clarification or add additional context in comments.

Comments

2
<input type="checkbox" id="hrm" name="hrm" value="yes" />


<?php

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

?>

2 Comments

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

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' />

2 Comments

Is value neccesary? I saw from firebug that without value it'll post something like "hrm: On".
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.