• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1739
  • Last Modified:

How to create a session onClick from a PHP Variable?

I have a variable called $ItemName and I have a link to another page.  I want to store the $ItemName in a session when it is clicked.  The reason is because there are several $ItemName links on the same page.  So the current link needs to somehow click and create a session right before it goes to the page.

Can anyone help me out with this?
Thank you
0
JuniorBee
Asked:
JuniorBee
2 Solutions
 
Michael701Commented:
why not just pass it as a $_GET variable?

echo "<a href='/next_page.php?item=".urlencode($ItemName)."'>Click me</a>";
0
 
Ray PaseurCommented:
I agree with Michael701 -- here is the general session handling strategy.

On EVERY PAGE at the top of the script put session_start();  I do that in a "config.php" file that I include as the first statement in every script I write.  By doing it every time, I know that the $_SESSION array is always available to my programs.  Because this is a superglobal array, it is available in all classes, methods and functions.

Then in "next_page.php" you would have code that looks like the snippet.

You might add some editing code to be sure that the URL string was acceptable for the rest of your processing.

HTH, ~Ray
<?php // RAY_get_to_session_example.php
error_reporting(E_ALL);
 
// ALWAYS START THE SESSION
session_start();
 
// COPY THE URL ARGUMENT INTO THE SESSION
$_SESSION["item"] = ''; // ASSUME NOTHING
if (!empty($_GET["item"]))
{
   $_SESSION["item"] = $_GET["item"];
}

Open in new window

0
 
JuniorBeeAuthor Commented:
Had to use javascript.
"ONCLICK"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now