简体   繁体   中英

How can I check if a checkbox is NOT checked

I need to check whether checkbox is checked or not. Normally I would do it like this:

<?php
    $checked = isset($_POST['checkbox']);
?>

But I don't know what is the name. More at screenshot (I'm using Laravel 4).

Screenshot

You simply can't. The data for unchecked checkboxes are not send to the server.

You could do a workaround with javascript where the JS appends some hidden fields before submit with the nonchecked boxes

Supposedly you should know what the list of checkboxes is/was that you asked the user to check. Checked checkboxes are submitted to the server, unchecked ones aren't. You can calculate the difference between these two lists.

如果您使用的是jquery,并且知道该复选框的ID,则可以使用以下代码进行检测:

var isChecked = $("#cbId").is(":checked");
<?php
if ( ! isset($_POST['checkbox_name']))
{
    "Not checked";
}
?>

If checkbox didn't check - you will not have this variable in $_REQUEST.

<form action="">
<input type="checkbox" name="ch1"/>
<input type="checkbox" name="ch2"/>
<input type="checkbox" checked="checked" name="ch3"/>
<input type="submit" name="Post" value="Post">
</form>

When you click on "Post". In backend you'll see:

<?php
if(isset($_REQUEST['ch1']))
    echo 'ch1 is checked!';
if(isset($_REQUEST['ch2']))
    echo 'ch2 is checked!';
if(isset($_REQUEST['ch3']))
    echo 'ch3 is checked!';
?>

In my case you'll see: "ch3 is checked!".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM