简体   繁体   中英

Get and set PHP variable based on anchor clicked

Is there a way to get a certain value and store it in a variable when an anchor linked to a div is clicked?

Like if I have two links like

<a href="#one">One</a>
<a href="#two">Two</a>

Would I be able to somehow get if the user clicked one or two and store it in a PHP variable?

Yes, you can add them as a GET request and store it in the session. Change your code this way:

<a href="?name=one">One</a>
<a href="?name=two">Two</a>

And in the PHP, you can do this way:

<?php
  session_start();
  $_SESSION["name"] = $_GET["name"];
?>

Take Praveen Kumar's solution and change the HTML part this way to keep anchors working:

<a href="?name=one#one">One</a>
<a href="?name=two#two">Two</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