如何从复选框获取未选中的值? [英] how to get the unchecked value from checkbox?

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

问题描述

我有一个复选框的列表,当我选中复选框,它应该将值插入数据库。当我取消选中复选框,那么它应该是从数据库中删除数据。

i got a list of checkbox, when i checked the checkbox, it should insert the value into database. when i unchecked the checkbox then it should be delete data from database.

如果我在复选框中分配一个值,那么我只能得到选中的复选框值。
如果我使用隐藏字段,然后我可以得到所有的复选框的值,但然后我dunno其中1是检查和哪个1是未选中。

if i assign a value in checkbox then i only can get the checked checkbox value. if i using a hidden field then i can get all value of checkbox but then i dunno which 1 is check and which 1 is uncheck.

任何人都可以帮助?

    $num="3";  
    for($i=1;$i<10;$i++){
    ?>
    <form name="form1" method="post" action="testcheckbox.php"> 
        <input type="hidden" name="task" value="validatesn">
        <input type="hidden" name="validate[]" value="<?php echo $i;?>">
        <input type="checkbox" name="validate[]" <?php if($num==$i){ echo "checked=checked";} ?> />Serialno<?php echo $i."<br/>"; ?>
    <?php
        $i++;
    }   
    ?>
        <input type="submit" name="submit" value="Validate" />
    </form>

    <?php
    if($_REQUEST['task'] == 'validatesn'){
       $valid=$_POST['validate'];
       foreach($valid as $v){
           echo $v;  //show all checkbox values
           //if checkbox= checked then insert value into database
           //if untick the checked checkbox then delete data from database
    }
  }
?>


推荐答案

复选框的标记如下: p>

The markup for a checkbox looks like so:

<input type="checkbox" value="myvalue" name="validate[]">

当你POST表单时,你会看到一个名为 validate $ _ POST

When you POST the form, you will see an array called validate in $_POST.

如果该数组为空,如果该数组包含myvalue,那么复选框被选中。

If that array is empty, then the checkbox was not checked. If that array contains "myvalue", then the checkbox was checked.

如果你正在处理多个复选框:

If you are dealing with multiple checkboxes like this:

<input type="checkbox" value="myvalue1" name="validate[]">
<input type="checkbox" value="myvalue2" name="validate[]">

您的脚本需要知道 validate 数组 $ _ POST 可以包含 myvalue1 myvalue2 。然后你可以查看 $ _ POST ['validate'] ,如果该值存在于数组中,则选中该复选框。您可以使用 array_diff()轻松地执行此操作,而无需编写任何循环。

Your script will need to know that the validate array in $_POST can contain values of myvalue1 and myvalue2. Then you can look into $_POST['validate'], and if the value exists in the array, then that checkbox was checked. You can use array_diff() to easily do this without writing any loops.

这篇关于如何从复选框获取未选中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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