What is the best approach for highlighting a clicked anchor link on a one-page site using PHP?
To highlight a clicked anchor link on a one-page site using PHP, you can add a class to the clicked link and use JavaScript to handle the smooth scrolling functionality. By adding a class to the clicked link, you can apply CSS styles to visually highlight the active link.
<?php
$current_page = basename($_SERVER['PHP_SELF']);
?>
<nav>
<a href="#section1" class="<?php echo ($current_page == 'index.php' && isset($_GET['section']) && $_GET['section'] == 'section1') ? 'active' : ''; ?>">Section 1</a>
<a href="#section2" class="<?php echo ($current_page == 'index.php' && isset($_GET['section']) && $_GET['section'] == 'section2') ? 'active' : ''; ?>">Section 2</a>
<a href="#section3" class="<?php echo ($current_page == 'index.php' && isset($_GET['section']) && $_GET['section'] == 'section3') ? 'active' : ''; ?>">Section 3</a>
</nav>