简体   繁体   中英

Checkbox checked with PHP form post?

How do I check a checkbox?

I've tried 1, On, and Yes. That doesn't work. Putting the worked "checked" alone works, but then how do I check with PHP after a form post of the checkbox is checked?

<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />

A checkbox will only be a successful control if it is checked.

Controls that are not successful are not submitted as data.

Therefore, you can tell if a checkbox is checked by seeing if its value has been submitted.

Eg

if ($_POST['chk']['newmsg2'] == 1) {

Here is the code;

<form action="test.php" method="POST">
    <input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
    <input type="submit">
</form>

<?php
    $check = $_POST['chk']['newmsg2'];
    echo "***$check****"
?>

If it is checked $check will show 1.

<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" <?php if ($_POST['chk']['newmsg2']): ?>checked="checked"<?php endif; ?> />

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