drcx71276 2010-11-15 12:53
浏览 29
已采纳

PHP:确定是否选中了复选框

My checkbox looks like this:

<input type="checkbox" name="activate[]" class="setSetting" value="<?php echo $row["id"]; ?>">

And then i have a foreach:

$activate = $_POST['activate']; 
        foreach($activate as $a){
        echo $a ."<br>";
        }

Works fine to get the value out of. But how can i determine if the checkbox has been checked?

$activate = $_POST['activate']; 
        foreach($activate as $a){
        $query_email = mysql_query("SELECT id FROM lp_email_settings ORDER BY id ASC");
            while($ro = mysql_fetch_row($query_email)){
            $getUserSettings = mysql_query("SELECT * FROM users_email_settings WHERE uID = '$USER' AND eSetting = '$ro[0]'");
                if($ro[0] == $a){
                    if(mysql_num_rows($getUserSettings) != 1){
                    mysql_query("INSERT INTO users_email_settings (uID, eSetting) VALUES ($USER, $ro[0])");
                    }
                }else{
                mysql_query("DELETE FROM users_email_settings WHERE uID = '$USER' AND eSetting = '$ro[0]'");
                }
            }
            echo $a."<br>";
        }
  • 写回答

4条回答 默认 最新

  • dtwupu6414 2010-11-15 12:56
    关注

    Only those checkboxes will be submitted that are considered successful (i.e. checked). That means only the checked checkboxes are available in $_POST['activate'].

    And to determine whether a checkbox has been checked, you can use array_search to check whether a particular ID value is in $_POST['activate']:

    array_search('12345', $_POST['activate'], true)
    

    And if you change your control’s name to use the ID as key like this:

    <input type="checkbox" name="activate[<?php echo $row["id"]; ?>]" class="setSetting" value="<?php echo $row["id"]; ?>">
    

    Then you can simply use isset or array_key_exists on $_POST['activate']:

    isset($_POST['activate']['12345'])
    array_key_exists('12345', $_POST['activate'])
    

    Edit    As already said in the comments, you should rather iterate the available options and check for each option whether it’s already active and needs to be activated or deactivated. You can do this as follows:

    $activate = array_flip($_POST['activate']);
    $query = "SELECT t1.id, t2.eSetting
              FROM lp_email_settings t1 LEFT OUTER JOIN users_email_settings t2 ON (t1.id = t2.eSetting)
              ORDER BY t1.id ASC";
    $result = mysql_query($query);
    $insert = array();
    $delete = array();
    while ($row = mysql_fetch_row($result)) {
        if ($row[1] === null) {
            // option is not set yet
            if (isset($activate[$row[0]])) {
                // option needs to be set
                $insert[] = $row[0];
            }
        } else {
            // option is already set
            if (!isset($activate[$row[0]])) {
                // option needs to be unset
                $delete[] = $row[0];
            }
        }
    }
    if (!empty($insert)) {
        $query = "INSERT INTO users_email_settings (uID, eSetting)
                  VALUES ($USER, " . implode("), ($USER, ", $insert) . ")";
        mysql_query($query);
    }
    if (!empty($delete)) {
        $query = "DELETE FROM users_email_settings
                  WHERE uID = $USER AND eSetting IN (" . implode(", ", $delete) . ")";
        mysql_query($query);
    }
    

    The first query will select a left join of all available options and the already active options. So the result set is the ID of the option in the first column and the second column is either NULL if the options is not active or otherwise again the ID. So if there are three options (e.g. 1, 2, and 3) and only the options 1 and 3 are already set, the result should look like this:

     id | eSetting
    ----+----------
      1 |        1
      2 |     NULL
      3 |        3
    

    So if $row[1] is null (inactive option), the ID in $row[0] is added to the $insert array if the corresponding option was set in the request ($activate[$row[0]], the keys/values are flipped to have a direct access). The same is done for those options that are already active but were not set in the request.

    At the end the collected options in $insert are inserted and those in $delete are removed.

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

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计