在我的php-header文档中,我希望能够高亮显示用户所在的当前页面,以防止混淆。
问题是,我使用了if(isset())函数来隐藏/展示导航链接,具体取决于用户是否登录。
但是,如何突出显示用户单击的页面?我想我必须在if(isset())函数的php-tag中包含一些javascript,然后将其连接到CSS内的活动类,还是我错了?
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>DreamcarZ</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Ubuntu+Condensed&display=swap" rel="stylesheet">
</head>
<body>
<div class="header">
<div class="topnav">
<div class="logo-container">
<h1>Dreamcar<span>Z</span></h1>
</div>
<?php
if(isset($_SESSION['userId'])){
echo '<a href="index.php">Home</a>
<a href="exclusive_cars.php">Collection</a>
<a href="om_oss.php">About</a>
<a href="#contact">Contact</a>';
}
else {
echo '<a href="index.php">Home</a>
<a href="om_oss.php">About</a>
<a href="#contact">Contact</a>';
}
?>
<div class="login-container">
<?php
if(isset($_SESSION['userId'])){
echo '<form action="includes/logout.inc.php" method="post">
<button type="submit" name="logout-submit">Logout</button>
</form>';
}
else {
echo '<form action="includes/login.inc.php" method="post">
<input type="text" name="mailuid" placeholder="Username...">
<input class="inpt-pwd" type="password" name="pwd" placeholder="Password...">
<button type="submit" name="login-submit">Login</button>
<a class="signup" href="signup.php">Signup</a>
</form>';
}
?>
</div>
</div>
</div>
最佳答案
您需要创建一个“活动”类,这将使nav元素看起来是唯一的。
然后,在每个页面上创建一个php变量作为标志值(特定于每个页面),然后使用if子句将该“ active”类分配给nav元素。
请参考本文以获取更多详细信息:
https://www.sitepoint.com/community/t/how-add-active-state-to-the-navigation-menu-with-php/7356
谢谢!