根据用户帐户隐藏菜单项

时间:2016-11-02 18:06:52

标签: javascript php jquery

在我的登录mysql表上,我有一个帐户类型列。如果用户具有管理员帐户类型,我想显示管理菜单项,如果不隐藏它。

这很简单但不起作用:

在我的标题中,我有以下内容:

<script>
var logged_in_account_type = "<?php echo $_SESSION['account_type'] ; ?>";
    if(logged_in_account_type === "Manager") {             
         document.getElementById('management_menu').style.display = 'block';
    } else {
        document.getElementById('management_menu').style.display = 'none';                          
    }
</script>

我试过没有回声很好。

<div id="management_menu" style="display:none">
  <li>
    <a href="index.php"><i class="menu-icon fa fa-dashboard"></i><span class="mm-text">Dashboard</span></a>
  </li>
</div>

enter image description here

enter image description here

<div class="text-bg"><span class="text-slim">Welcome,</span> <span class="text-semibold"><?php echo $_SESSION['account_type'] ?></span></div>

2 个答案:

答案 0 :(得分:3)

在渲染菜单时,您是否尝试过使用php if ?像这样:

  <?php if($_SESSION['account_type'] == 'Manager'): ?>
<div id="management_menu">
 <ul>
  <li>
    <a href="index.php">Dashboard</a>
  </li>
    <li>
      <a href="index.php">Users</a>
    </li>
    </ul>
</div>  
  <?php endif; ?>

答案 1 :(得分:0)

文档中的javascript代码在哪里?如果您的javascript代码位于文档的顶部,则可能会正确显示或隐藏<div>元素,但是,因为<div>元素位于文档的底部,所以它被隐藏,因为它的& #34;风格&#34;这么说。

解决方案是将javascript代码移动到 <div id="management_menu" style="display:none"> <li> <a href="index.php"><i class="menu-icon fa fa-dashboard"></i><span class="mm-text">Dashboard</span></a> </li> </div> <?php session_start(); $_SESSION['account_type'] = "Manager"; ?> <script> var logged_in_account_type = "<?php echo $_SESSION['account_type'] ; ?>"; if(logged_in_account_type === "Manager") { document.getElementById('management_menu').style.display = 'block'; } else { document.getElementById('management_menu').style.display = 'none'; } </script> 元素下面,这将解决&#34;执行顺序&#34;你有问题。示例:

def updateScoreBoard():
     font=pygame.font.SysFont(None,14)
     text=font.render('Score: %05d' (SCORE),True,pygame.Color(255,255,255))
     xl, ytop, xr, ybottom = textRect = text.get_rect()
     mainSurface.blit(text,textRect)

     text=font.render('Egg Caught: %03d' (EGGSCAUGHT), True, pygame.Color(255,255,255))
     xl, yt, xr, yb = textRect = text.get_rect()
     yt += (int(ybottom * 1.5) + ytop)
     yb += (int(ybottom * 1.5) + ytop)
     textRect = xl, yt, xr, yb
     mainSurface.blit(text,textRect)