如何使用 php 检测未选中的复选框?

How can I detect unchecked checkbox with php?(如何使用 php 检测未选中的复选框?)
本文介绍了如何使用 php 检测未选中的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的表单中有 3 个(我不知道有多少是可以更改的.只有 3 个示例)复选框,我想在发布时使用 php 检测未选中的复选框.我该怎么做?

There are 3(I do not know how many would be it changeable. 3 only example) checkboxes in my form and I want to detect unchecked checkboxes with php when it post. How can I do this?

推荐答案

秋葵汤是对的.但是,有一个解决方法,如下所示:

Gumbo is right. There is a work around however, and that is the following:

<form action="" method="post">
    <input type="hidden" name="checkbox" value="0">
    <input type="checkbox" name="checkbox" value="1">
    <input type="submit">
</form>

换句话说:有一个与复选框同名的隐藏字段和一个表示未选中状态的值,例如 0.然而,重要的是让隐藏字段位于表单中的复选框之前.否则,如果复选框被选中,隐藏字段的值将在发布到后端时覆盖复选框值.

In other words: have a hidden field with the same name as the checkbox and a value that represents the unchecked state, 0 for instance. It is, however, important to have the hidden field precede the checkbox in the form. Otherwise the hidden field's value will override the checkbox value when posted to the backend, if the checkbox was checked.

另一种跟踪此情况的方法是在后端有一个可能的复选框列表(例如,甚至可以在后端使用该列表填充表单).类似下面的内容应该会给你一个想法:

Another way to keep track of this is to have a list of possible checkboxes in the back-end (and even populate the form in the back-end with that list, for instance). Something like the following should give you an idea:

<?php

$checkboxes = array(
    array( 'label' => 'checkbox 1 label', 'unchecked' => '0', 'checked' => '1' ),
    array( 'label' => 'checkbox 2 label', 'unchecked' => '0', 'checked' => '1' ),
    array( 'label' => 'checkbox 3 label', 'unchecked' => '0', 'checked' => '1' )
);

if( strtolower( $_SERVER[ 'REQUEST_METHOD' ] ) == 'post' )
{
    foreach( $checkboxes as $key => $checkbox )
    {
        if( isset( $_POST[ 'checkbox' ][ $key ] ) && $_POST[ 'checkbox' ][ $key ] == $checkbox[ 'checked' ] )
        {
            echo $checkbox[ 'label' ] . ' is checked, so we use value: ' . $checkbox[ 'checked' ] . '<br>';
        }
        else
        {
            echo $checkbox[ 'label' ] . ' is not checked, so we use value: ' . $checkbox[ 'unchecked' ] . '<br>';
        }
    }
}
?>
<html>
<body>
<form action="" method="post">
    <?php foreach( $checkboxes as $key => $checkbox ): ?>
    <label><input type="checkbox" name="checkbox[<?php echo $key; ?>]" value="<?php echo $checkbox[ 'checked' ]; ?>"><?php echo $checkbox[ 'label' ]; ?></label><br>
    <?php endforeach; ?>
    <input type="submit">
</form>
</body>
</html>

...勾选一两个复选框,然后点击提交按钮,看看会发生什么.

... check one or two checkboxes, then click the submit button and see what happens.

这篇关于如何使用 php 检测未选中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)