简体   繁体   English

如何检查是否只选中了一个或两个复选框

[英]How can i check if only one or both checkboxes are checked

I have to check if one checkbox is checked or both. 我必须检查是否选中了一个复选框,或者两个都选中。 But the problem is, if i try my code. 但是问题是,如果我尝试我的代码。 The site has an error ("This site doesn't work"). 该网站有错误(“此网站无法正常工作”)。

I tried this: 我尝试了这个:

    $lw1l = $_POST['lw1l'];
    $lw1s = $_POST['lw1s'];
    $lw2l = $_POST['lw2l'];
    $lw2s = $_POST['lw2s'];
    if(isset($lw1l)){
      if(isset($lw1s)){
        $lw1 = "read / write";
      }else{
        $lw1 = "read ";
      }
    }else{
      isset($lw1s){
        $lw1 = "write";
      }
    }

    if(isset($lw2l)){
      if(isset($lw2s)){
        $lw2 = "read/ write";
      }else{
        $lw2 = "read";
      }
    }else{
      if(isset($lw2s)){
        $lw2 = "write";
      }
    }

But this doesn't work... 但这行不通...

Would be glad if somebody could help :D 如果有人可以帮助您会很高兴:D

Here is a version that will be easier to read and maintain: 这是一个易于阅读和维护的版本:

function getPermissions($key){
    $s_is_set = isset($_POST[$key."s"]);
    return isset($_POST[$key."l"])
        ? $s_is_set
            ? 'read / write'
            : 'read' 
        : 'write';
}

$lw1 = getPermissions('lw1');
$lw2 = getPermissions('lw2');

Don't declare variables because if user doesn't click on checkbox, the variable doesn't exist. 不要声明变量,因为如果用户未单击复选框,则该变量不存在。

Try this : 尝试这个 :

if(isset($_POST['lw1l'])){
    if(isset($_POST['lw1s'])){
        $lw1 = "read / write";
    }else{
        $lw1 = "read ";
    }
}else{
    if(isset($_POST['lw1s'])){
        $lw1 = "write";
    }
}

if(isset($_POST['lw2l'])){
    if(isset($_POST['lw2s'])){
        $lw2 = "read/ write";
    }else{
        $lw2 = "read";
    }
}else{
    if(isset($_POST['lw2s'])){
        $lw2 = "write";
    }
}

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

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