如果不是管理员,请隐藏按钮

What I want is something like that :

If not admin then hide some button. But if I do

if ($Admin == null)
   {
       echo "You are not admin"; //That works
       //So we hide the button id="New"
?>      
    <script type = "text/javascript">
        document.getElementById('New').style.visibility = "hidden";     
    </script>
<?php } ?>

But nothing happens. It says "You are not admin" but the button isn't hidden

I know how to hide fields by clicking buttons or checking checkbox with functions but i don't know how how to do by using a "if". Thanks for reading

You should not just hide the button but not echo it at all. Unless the "not admin" can change the html and get access to admin features, in other words your web page can get hacked

Your best bet is to simply not output the button with the id "New" at all.

Very much second best:

If your code isn't working, it means one of these things:

  1. The button doesn't exist yet when the script runs. Output it later, at the end of the page, after you've output the button.

  2. The button doesn't have id="New" on it. Either give it an ID, or if I guess that "New" is the value, you can do document.querySelector("input[type=button][value=New]") rather than document.getElementById.


Irrespective of whether you output the button or hide it, it's vital that whatever server-side code it is that runs when the button is pressed also checks that the user is an admin and disregards the action, since requests can be faked. You may well already be doing that, but I thought I'd flag it up.

Display the button if and only if the user is admin:

<?php if (!!$Admin) { ?>
    <!-- display the button -->
<?php } ?>