Suika
Suika

Reputation: 660

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?

Upvotes: 0

Views: 730

Answers (2)

jossif
jossif

Reputation: 409

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>

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

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"];
?>

Upvotes: 1

Related Questions