'How to hide buttons based on your "job"?
I am creating an app and I want that the buttons appear based on your $job
. There are 4 jobs, which are all in mysql databases:
- student
- teacher
- staff
- principal,
The signup button can only be seen by teacher, staff and principal.
But I don't know how to do it.
Here is my code:
<?php
session_start();
include("connection.php");
include("functions.php");
$user_data = check_login($con);
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$job = $_POST['job'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title> NetPlat </title>
</head>
<body>
<a href="login.php">
<button id="button">Logout</button>
</a><br><br>
<a href="signup.php">
<button>Signup Student</button>
</a>
</body>
</html>
Solution 1:[1]
Surrounding the button markup with a simple if
statement using the in_array
function to check that the job is allowed, will work.
<?php
if(in_array($job, ['teacher', 'staff', 'principal']))
{
?>
<a href="signup.php">
<button>Signup Student</button>
</a>
<?php
}
?>
The above will only output the button html if the value of $job
is found in the array.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Arleigh Hix |