Is it possible to do an IF with direct javascript in html?

-4

I have a button that I want to show only if the person logged in is an administrator. But it has to be in JS.

In PHP, I would do this:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>

<body>

    <?php if (loginPermissoes == "admin"){ ?>
        <button id="btnOcorrenciaConcluida">Marcar como concluído</button>
    <?php }else{ ?>

    <?php } ?>


</body>
</html>

How do I do this in Javascript or with jQuery?

Would it be this way?

<script> if (loginPermissoes == "admin"){ </script>
    <button id="btnOcorrenciaConcluida">Marcar como concluído</button>
<script> }else{ </script>

<script> } </script>
    
asked by anonymous 28.12.2015 / 13:57

4 answers

4

Doing this only on the front-end will not bring any security. Any malicious person will be able to control things as long as he has the minimum of knowledge, anyway you can use combined with css , like this:

  

Note: I only used window.prompt as an example, you can control the variable as you want.

var btnOcorrenciaConcluida = document.getElementById("btnOcorrenciaConcluida");
var permissoesLogin = window.prompt("Qual permissão?");

if (permissoesLogin == "admin") {
   btnOcorrenciaConcluida.className = "";
} else {
   btnOcorrenciaConcluida.className = "hide";
}
.hide {
    display: none;
}
<button id="btnOcorrenciaConcluida" class="hide">Marcar como concluído</button>

The whole code looks like this:

<html>
<head>
    <style>
    .hide {
         display: none;
    }
    </style>
</head>
<body>
    <button id="btnOcorrenciaConcluida" class="hide">Marcar como concluído</button>

    <script type="text/javascript">
        var btnOcorrenciaConcluida = document.getElementById("btnOcorrenciaConcluida");
        var permissoesLogin = window.prompt("Qual permissão?");

        if (permissoesLogin == "admin") {
           btnOcorrenciaConcluida.className = "";
        } else {
           btnOcorrenciaConcluida.className = "hide";
        }
    </script>
</body>
</html>
    
28.12.2015 / 14:21
2

I do not quite understand what you want. Do you want to use variables made in PHP by JS? Without using PHP in the middle? So I think you want to use Ajax .

Basically I've already done an old function to make an Ajax request . You can use them:

function Get(t,e,n){var s=new XMLHttpRequest;s.onreadystatechange=function(){4==s.readyState&&200==s.status?(e(s.responseText),s=void 0):s.status>=500&&(s=void 0,n(0))},t+=t.indexOf("?")>-1?"&tsmp="+Date.now():"?tsmp="+Date.now(),s.open("GET",t,!0),s.send()}
function Post(t,e,n,o){var s=new XMLHttpRequest;s.onreadystatechange=function(){4==s.readyState&&200==s.status?(n(s.responseText),s=void 0):s.status>=500&&(s=void 0,o(0))},t+=t.indexOf("?")>-1?"&tsmp="+Date.now():"?tsmp="+Date.now(),s.open("POST",t,!0),s.setRequestHeader("Content-type","application/x-www-form-urlencoded"),s.send(e)}

(b, well, I had compressed before)

And, well, you need a PHP file that echoes some of the user's value. As an example:

<?php (...) echo Usuario["privilegio"];?>

Then, using the Get function above you can get this value that will exit the PHP file. 3 arguments are required: [URL do arquivo] , [função de sucesso] and [função de erro]

Get("http://localhost:8000/valor.php",function(e){
      alert("Valor: "+e);//alerta "Valor: Usuario[\"privilegio\"]" do arquivo PHP
      if(parseInt(e)==0){//se o privilégio do usuário é igual à 0
         //yay
      }
    },function(){
      alert("Falhou na requisição. Pode ser um delay?")
    }
)

Remember to make the same if conditon for the admin action file.

    
28.12.2015 / 14:27
1
<script type="text/javascript">
    var permissoesLogin = '<?php echo $loginPermissoes; ?>';
    if (permissoesLogin == "admin") {
         //todo
    } else {
         //todo
    }
</script>
    
28.12.2015 / 14:03
-1

You will not be able to deflect the flow with javascript in this way you did.

You can insert the html into javascript

<script type="text/javascript">
var btn = '<button id="btnOcorrenciaConcluida">Marcar como concluído</button>';
if (permissoesLogin == "admin") {
     // comando para escrever o btn em alguma tag html
} else {
     // faz outra coisa...
}

    
28.12.2015 / 14:20