我必须检查是否选中了一个复选框,或者两个都选中。但是问题是,如果我尝试我的代码。该网站有错误(“此网站无法正常工作”)。
我尝试过:
$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";
}
}
但这不起作用...
如果有人可以帮助:D
答案 0 :(得分:2)
这是一个易于阅读和维护的版本:
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');
答案 1 :(得分:1)
不声明变量,因为如果用户未单击复选框,则该变量不存在。
尝试一下:
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";
}
}