Alex Rivera | Logout

How to set a session variable when clicking a <a> link

Asked 2011-01-31T18:49:29.283
16

I have the following problem... I want to set a session variable when clicking on a normal link like:

<a href="home" name="home">home</a>

My research seems to point out that it is not possible for PHP to catch up with the click event in a such a way that it would set a session variable.

I believe it is possible with Ajax, but how? And what would my link look like?
Setting the session variable should look like:

$_SESSION['link'] = home;

So in short: When clicking on a link in HTML, a session variable must be set.
HOW am i going to do that?

PS: I'm not quite familiar with Ajax, but I'll catch up.

EDIT: The links will refer to the same page, also i want to avoid urls like "home.php?link=X".
If it isn't possible to do it any other way, too bad. But I'll hope there is a solution.
Important: the name of the link will be the value of $_SESSION['link']

Edit
Report

1 Answer

12

I had the same problem - i wanted to pass a parameter to another page by clicking a hyperlink and get the value to go to the next page (without using GET because the parameter is stored in the URL).

to those who don't understand why you would want to do this the answer is you dont want the user to see sensitive information or you dont want someone editing the GET.

well after scouring the internet it seemed it wasnt possible to make a normal hyperlink using the POST method.

And then i had a eureka moment!!!! why not just use CSS to make the submit button look like a normal hyperlink??? ...and put the value i want to pass in a hidden field

i tried it and it works. you can see an exaple here http://paulyouthed.com/test/css-button-that-looks-like-hyperlink.php

the basic code for the form is:

    <form enctype="multipart/form-data" action="page-to-pass-to.php" method="post">
                        <input type="hidden" name="post-variable-name" value="value-you-want-pass"/>
                        <input type="submit" name="whatever" value="text-to-display" id="hyperlink-style-button"/>
                </form>

the basic css is:

    #hyperlink-style-button{
      background:none;
      border:0;
      color:#666;
      text-decoration:underline;
    }

    #hyperlink-style-button:hover{
      background:none;
      border:0;
      color:#666;
      text-decoration:none;
      cursor:pointer;
      cursor:hand;
    }
answered 2012-04-01T15:11:16.463

Your Answer