-1

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

Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23
kevwpl
  • 57
  • 7
  • Can you paste your HTML Code as well. Also what is the Error. – ascsoftw Jul 12 '19 at 09:46
  • Possible duplicate of [PHP's white screen of death](https://stackoverflow.com/questions/1475297/phps-white-screen-of-death) – misorude Jul 12 '19 at 09:46
  • Your code contains syntax errors. Go check the mentioned duplicate (pay attention specifically to this answer, https://stackoverflow.com/a/33665510/10283047) on how to configure your PHP error reporting properly, so that PHP can tell you what is wrong. – misorude Jul 12 '19 at 09:46
  • What should happen if no checkboxes are checked? – Qirel Jul 12 '19 at 09:56

2 Answers2

2

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');
Dov Rine
  • 810
  • 6
  • 12
1

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";
    }
}
jobouille
  • 49
  • 2