Reputation: 476
I'm working on a small project to get a basic website done and I can't figure out how in php I get my menu navbar to change to Logout as soon as the user has successfully signed in.
I've pasted the navbar code and all the code I think which may be relevant. Any help or guidance would be appreciated. Quite new to php, you may need to see all the code below so apologise if not much help. The PHP code was used from a YouTube tutorial....
NavBar Menu Code
iStudy University
<section class="collapse navbar-collapse" id="myTogglerNav">
<div class="navbar-nav ml-auto">
<a class="nav-item nav-link px-3" href="index.php"><i class="fas fa-home"><span class="nav-text"> Home</span></i></a>
<a class="nav-item nav-link px-3" href="about.html"><i class="fas fa-info-circle"><span class="nav-text"> About</span></i></a>
<a class="nav-item nav-link px-3" href="contact.html"><i class="fas fa-envelope"><span class="nav-text"> Contact Us</span></i></a>
<a class="nav-item nav-link px-3" href="signup.php"><i class="fas fa-check-square"><span class="nav-text"> Sign Up</span></i></a>
<a class="nav-item nav-link px-3" href="login.php"><i class="fas fa-sign-in-alt"><span class="nav-text"> Login</span></i></a>
</div>
</section>
</div>
Some PHP which is applied:
<?php
if (!isset($_SESSION['id'])) {
echo '<form action="includes/login.inc.php" method="post">
<input type="text" name="mailuid" placeholder="Email">
<input type="password" name="pwd" placeholder="Password">
<button type="submit" name="login-submit">Login</button>
</form>';
}
else if (isset($_SESSION['id'])) {
echo '<form action="includes/logout.inc.php" method="post">
<button type="submit" name="logout-submit">Logout</button>
</form>';
}
?>
The signup form code:
<form class="form-signup" action="includes/signup.inc.php" method="post">
<?php
// Here we check if the user already tried submitting data.
// We check username.
if (!empty($_GET["uid"])) {
echo '<input type="text" name="uid" placeholder="Username" value="'.$_GET["uid"].'">';
}
else {
echo '<input type="text" name="uid" placeholder="Username">';
}
// We check e-mail.
if (!empty($_GET["mail"])) {
echo '<input type="text" name="mail" placeholder="Email" value="'.$_GET["mail"].'">';
}
else {
echo '<input type="text" name="mail" placeholder="Email">';
}
?>
<input type="password" name="pwd" placeholder="Password">
<input type="password" name="pwd-repeat" placeholder="Repeat password">
<button type="submit" name="signup-submit">Signup</button>
</form>
Upvotes: 1
Views: 1552
Reputation: 296
Do the same that's been done for your form on the <a>
tag in your navigation.
<section class="collapse navbar-collapse" id="myTogglerNav">
<div class="navbar-nav ml-auto">
<a class="nav-item nav-link px-3" href="index.php"><i class="fas fa-home"><span class="nav-text"> Home</span></i></a>
<a class="nav-item nav-link px-3" href="about.html"><i class="fas fa-info-circle"><span class="nav-text"> About</span></i></a>
<a class="nav-item nav-link px-3" href="contact.html"><i class="fas fa-envelope"><span class="nav-text"> Contact Us</span></i></a>
<a class="nav-item nav-link px-3" href="signup.php"><i class="fas fa-check-square"><span class="nav-text"> Sign Up</span></i></a>
<?php
if (!isset($_SESSION['id'])) {
echo "<a class='nav-item nav-link px-3' href='login.php'><i class='fas fa-sign-in-alt'><span class='nav-text'> Login</span></i></a>";
} else {
echo "<a class='nav-item nav-link px-3' href='logout.php'><i class='fas fa-sign-out-alt'><span class='nav-text'> Logout</span></i></a>";
}
?>
</div>
</section>
Upvotes: 1