dongzhanlu8890 2014-09-24 09:53 采纳率: 0%
浏览 69
已采纳

计数已选中复选框(数组)编号并保存选中的复选框

I have an array of checkbox in a table in PHP file like this :

echo "<td $Blocked><input type =\"checkbox\" name=\"Blocked[]\" value=\"checkblock\" /></td>";

I am trying to get the value of number of checked checkboxes and save it to DB.

$Blocked = 'unchecked';

if ((isset($_POST['edit_tc']))) {
    if (isset($_POST['Blocked'])) {
        if (is_array($_POST['Blocked'])) {
            foreach($_POST['Blocked'] as $value) {
                error_log($value);
            }
        }
        else {
            $value = $_POST['Blocked'];
            error_log($value);
        }

        $Blocked = 'checked';
    }
}

"edit_tc" is the Submit button.

How do I take the number of it when the user checks the checkbox & clicks Submit button to save it to a table column?

  • 写回答

3条回答 默认 最新

  • doulan8846 2014-09-24 10:42
    关注

    I guess below code will solve all your problem.

    <?php
    $hello = array();
    if(isset($_POST['submit'])) {
      extract($_POST);
      print_r($hello); //print all checked elements
    }
    ?>
    
    <form method="post">
        <input type="checkbox" name="hello[]" value="1" <?php if(in_array(1, $hello)){ echo 'checked'; } ?>>
        <input type="checkbox" name="hello[]" value="2" <?php if(in_array(2, $hello)){ echo 'checked'; } ?>>
        <input type="checkbox" name="hello[]" value="3" <?php if(in_array(3, $hello)){ echo 'checked'; } ?>>
        <input type="checkbox" name="hello[]" value="4" <?php if(in_array(4, $hello)){ echo 'checked'; } ?>>
        <button type="submit" name="submit" value="Submit">Submit</button>
    </form>
    

    You have to check the value of each checkbox inside the checkbox array you are getting using in_arrayfunction of PHP.

    If you need to understand whole thing, just let me know.

    Will be glad to help you out.

    Regards.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试