[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get Session value through JQuery

Posted on 2009-12-23
5
Medium Priority
?
2,620 Views
Last Modified: 2012-05-08
Is there any way to get Session value through JQuery?
0
Comment
Question by:rpkhare
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
nergik earned 2000 total points
ID: 26111866
Yes there is, but as the browser doesnt have the Session values (i guess you mean PHP $_SESSION) you will need to do an Ajax call  using jQuery to a php script,

What i recommend you is to use JSON format to exchange info between js and the server, so you will need to do the following,

JS side
----------
$.getJSON("http://yourserver/sessionVarGet.php?name=whatever",
        function(data){
         
            alert(data);  //do whatever you want with it...
        });


Server Side - script http://yourserver/sessionVarGet.php
------------------------------------------------------------------------

$varname = $_GET['name'];
$out = $_SESSION['$varname];

echo json_encode($out);


And thats it !!






0
 
LVL 8

Author Comment

by:rpkhare
ID: 26111939
I've never used JSON. Do I need to include any .js or other library file to make it work?
0
 
LVL 2

Expert Comment

by:nergik
ID: 26112109
No, you don't need any library, JSON is a standard javascript object notation
  http://en.wikipedia.org/wiki/JSON

0
 
LVL 8

Author Comment

by:rpkhare
ID: 26117508
@nergik: You mean, instead of JQuery, I need to use JSON? The code that you illustrated above will do?
0
 
LVL 8

Author Closing Comment

by:rpkhare
ID: 31669388
Thanks. Excellent solution. It worked seemlessly.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question