简体   繁体   中英

Autoincrement url for anchor link

I am trying to make anchor links for different sections in the page.

#section1 #section2

But, the url isn't being updated each time I click the href link

 if(!isset($_SESSION['counter'])) {
     $_SESSION['counter'] = 1;
 }
 <?php echo '<a href = "#section'.($_SESSION['counter']++).'">'.'Next Section'.'</a>';?>

When I echo the counter I can see it being updated upon refresh but I want to be able update the anchor section without refreshing.

Thank you

You need to actually increment the value in the session and not just what is currently stored in the session.

 // If there is a value in the session, then increment it. 
 if(isset($_SESSION['counter'])) {
     $_SESSION['counter'] = $_SESSION['counter'] + 1;
 } else {
     $_SESSION['counter'] = 1;
 }

// Now, use the value which you have set. 
<?php echo '<a href = "#section'.($_SESSION['counter']).'">'.'Next Section'.'</a>';?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM