简体   繁体   English

显示已选中的复选框

[英]Display which check boxes have been checked

I have an array of numbers and would like to create a check box for each element in the array. 我有一个数字数组,想为该数组中的每个元素创建一个复选框。 Once the submit button is pressed the check boxes which were selected will be displayed. 一旦按下提交按钮,将显示选中的复选框。 Is there a way to do this? 有没有办法做到这一点?

PHP code : PHP代码:

<?php

    $arr = array("1","2","3","4");

    //Create a checkbox for each array element
    foreach($arr as $index=>$value)
    {
    echo ' <input type="checkbox" name = "checkb[]"value="'.$value.'">'; 

    }

    echo'<input type = "submit" name = "submit" value = "submit">';

    if(isset($_POST['submit'])){
    foreach($_POST['checkb'] as $checkb)
    {
    echo $checkb." ";
    }
    }
    ?>

you dont have any form to post checkbox value, use form tag and, you can use in_array() for checked checkbox: 您没有任何formpost checkbox值,使用form标记,并且可以使用in_array()来选中复选框:

<?php

    $arr = array("1","2","3","4");

    //Create a checkbox for each array element
    foreach($arr as $index=>$value)
    {
    echo '<form action="" method="post">';
    echo ' <input type="checkbox" name = "checkb[]"value="'.$value.'">'; 

    }

    echo'<input type = "submit" name = "submit" value = "submit"></form>';

    if(isset($_POST['submit'])){
    foreach($_POST['checkb'] as $checkb)
    {
     if(in_array($checkb,$arr))
    {
    echo '<input type="checkbox" name = "checkb[]"value="'.$checkb.'" checked>';
    }
    }
    }
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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