How to read if a checkbox is checked in PHP?

2018-12-31 15:02发布

How to read if a checkbox is checked in PHP?

16条回答
余欢
2楼-- · 2018-12-31 15:17

Zend Framework use a nice hack on checkboxes, which you can also do yourself:

Every checkbox generated is associated with a hidden field of the same name, placed just before the checkbox, and with a value of "0". Then if your checkbox as the value "1", you'll always get the '0' or '1' value in the resulting GET or POST

<input type="hidden" name="foo" value="0" />
<input type="checkbox" name="foo" value="1"> 
查看更多
若你有天会懂
3楼-- · 2018-12-31 15:26
$check_value = isset($_POST['my_checkbox_name']) ? 1 : 0;
查看更多
牵手、夕阳
4楼-- · 2018-12-31 15:26

Well, the above examples work only when you want to INSERT a value, not useful for UPDATE different values to different columns, so here is my little trick to update:


//EMPTY ALL VALUES TO 0 
$queryMU ='UPDATE '.$db->dbprefix().'settings SET menu_news = 0, menu_gallery = 0, menu_events = 0, menu_contact = 0';
            $stmtMU = $db->prepare($queryMU);
            $stmtMU->execute();
if(!empty($_POST['check_menus'])) {
    foreach($_POST['check_menus'] as $checkU) {
try {
//UPDATE only the values checked
    $queryMU ='UPDATE '.$db->dbprefix().'settings SET '.$checkU.'= 1';
            $stmtMU = $db->prepare($queryMU);
            $stmtMU->execute();  
        } catch(PDOException $e) {
          $msg = 'Error: ' . $e->getMessage();}

        }
}
<input type="checkbox" value="menu_news" name="check_menus[]" />
<input type="checkbox" value="menu_gallery" name="check_menus[]" />

....

The secret is just update all VALUES first (in this case to 0), and since the will only send the checked values, that means everything you get should be set to 1, so everything you get set it to 1.

Example is PHP but applies for everything.

Have fun :)

查看更多
心情的温度
5楼-- · 2018-12-31 15:26

I've been using this trick for several years and it works perfectly without any problem for checked/unchecked checkbox status while using with PHP and Database.

HTML Code: (for Add Page)

<input name="status" type="checkbox" value="1" checked>

Hint: remove "checkbox" if you want to show it as unchecked by default

HTML Code: (for Edit Page)

<input name="status" type="checkbox" value="1" 
<?php if ($row['status'] == 1) { echo "checked='checked'"; } ?>>

PHP Code: (use for Add/Edit pages)

$status = $_POST['status'];
if ($status == 1) {
$status = 1;
} else {
$status = 0;
}

Hint: There will always be empty value unless user checked it. So, we already have PHP code to catch it else keep the value to 0. Then, simply use the $status variable for database.

查看更多
低头抚发
6楼-- · 2018-12-31 15:28

A minimalistic boolean check with switch position retaining

<?php

$checked = ($_POST['foo'] == ' checked');

?>

<input type="checkbox" name="foo" value=" checked"<?=$_POST['foo']?>>
查看更多
余欢
7楼-- · 2018-12-31 15:30

To check if a checkbox is checked use empty()

When a form is submitted, the checkbox will ALWAYS be set, because ALL POST variables will be send with the form.

Check if checkbox is checked with empty as followed:

//Check if checkbox is checked    
if(!empty($_POST['checkbox']){
#Checkbox selected code
} else {
#Checkbox not selected code
}
查看更多
登录 后发表回答