如何检测未选中复选框与PHP? [英] How can I detect unchecked checkbox with php?

查看:131
本文介绍了如何检测未选中复选框与PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有3(我不知道有多少会是可变的。3只有示例)复选框在我的形式,我想检测未经检查的复选框与php当它发布。

can-i-detect-unchecked-checkbox-with-php#13542924> Gumbo是对的。有一个工作,但是,这是以下:

 < form action =method =post> ; 
< input type =hiddenname =checkboxvalue =0>
< input type =checkboxname =checkboxvalue =1>
< input type =submit>
< / form>

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



另一种跟踪此问题的方法是列出可能的后台复选框(例如,甚至填充后端列表中的表单)。像下面这样应该给你一个想法:

 <?php 

$ checkboxes = array (
array('label'=>'checkbox 1 label','unchecked'=>'0','checked'=>'1'),
array >'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']。 '被检查,所以我们使用值:'。 $ checkbox ['checked']。 '< br>';
}
else
{
echo $ checkbox ['label']。 '未被选中,因此我们使用值:'。 $ checkbox ['unchecked']。 '< br>';
}
}
}
?>
< html>
< body>
< form action =method =post>
<?php foreach($ checkboxes as $ key => $ checkbox):?>
< label>< input type =checkboxname =checkbox [<?php echo $ key;?>]value =<?php echo $ checkbox ['checked']; ?>><?php echo $ checkbox ['label']; ?>< / label>< br>
<?php endforeach; >
< input type =submit>
< / form>
< / body>
< / html>

...检查一两个复选框,然后单击提交按钮,看看会发生什么。 p>

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>

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆