omg
omg

Reputation: 140112

How to judge whether <input type="checkbox" /> is checked on with PHP?

<form method="POST">

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

</form>

I mean when the form is posted.

Upvotes: 0

Views: 4621

Answers (3)

lfx
lfx

Reputation: 1391

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

Upvotes: 0

Amber
Amber

Reputation: 527538

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

Upvotes: 4

meder omuraliev
meder omuraliev

Reputation: 186762

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


<?php

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

?>

Upvotes: 2

Related Questions