How to write a link like <a href="#id"> which link to the same page in PHP?
In PHP, you can use the echo
function to output an HTML <a>
tag with a "#id" value for the href
attribute, which will link to the same page. For example:
echo '<a href="#id">Link to same page</a>';
This will output an HTML link that, when clicked, will take the user to the same page with the element with the matching id of "#id"
<a href="#id">Link to same page</a>
Watch a video course
Learn object oriented PHP
You can then use javascript to scroll the page to the id
<script>
document.querySelector('a[href="#id"]').addEventListener('click', function(event) {
event.preventDefault();
document.getElementById('id').scrollIntoView({
behavior: 'smooth'
});
});
</script>
This will scroll the page smoothly to the element with the matching id of "#id"