Show and hide DIV according to user level

Asked

Viewed 1,054 times

0

I need to display some elements div according to the level of the logged in user. If the usurer is level one can only sample certain div's. The exhibition below:

<div id="hmn0" style="margin-top:0px;">
    <nav>
        <div id="acdnmenu" style="width:199px;height:390px;">
            <ul>
              <li id="v_sm">Administrativo
                    <ul>
                        <li><a href="./adm/cd_reqequip.php" target="main">Requisição de equipamentos</a></li>
                        <li><a href="./adm/cd_document.php" target="main">Documentações</a></li>
                        <li><a href="./cadastro.php" target="main">Cadastro</a></li>
                    </ul>
                </li>
                <li id="v_tt">Faturamento
                    <ul>
                        <li><a href="./faturamento/cd_guiahm.php" target="main">Honorário Médico</a></li>
                        <li><a href="./faturamento/cd_lancsessao.php" target="main">Lançar sessões</a></li>
                        <li><a href="./faturamento/cad_lancconta.php" target="main">Lançar na conta</a></li>
                        <li><a href="./faturamento/fat_unidade.php" target="main">Fatura por unidades</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </nav>
</div>
  • In my case in . NET created HTML in the back end and rendered as per the user level rule.

  • I posted a very generic solution, there may be numerous variations according to your specific context. If I can provide more details, I can refine my solution.

2 answers

2

A very general implementation could be:

function exibirDivDeAcordoComNivelDoUsuario(usuario) {
    if(usuario.nivel !== 1) {
        document.getElementById("hmn0").style.display = 'none';
    }
}
  • I have a system with user levels, which are three levels and after the login screen enter the MENU page, a page that has several DIV’s with links to other pages... but I would like only appear certain DIV’s according to the level of the user who logged ... I’m using PHP and HTML ... I have already managed to block so that the user does not access the page depending on the level of it, but the DIV on the MENU page still appears.

1

As vc did not specify in which server environment your site is, I will give a simple example in ASP (Vbscript):

I create a Session with the user level when he logs in:

session("user_nivel") = 1

In HTML, I only show level 1 what I want, like this:

<%if session("user_nivel") >= 2 then%>
<div>
  Esta div só é visível para nível 2 acima
</div>
<%end if%>

<%if session("user_nivel") = 1 then%>
<div>
  Esta div é visível somente para nível 1
</div>
<%end if%>

If you are using PHP, the idea is the same.

Do not do this in Javascript because it can be swindled. The best is that the div is already absent from the server.

  • I think I’m doing something wrong, in php I’m leaving it <?php session_start (); $_SESSION['Usuarionivel'] = 1; ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.