Jump to content

Getting $_SESSION variable through JS?


khanahk

Recommended Posts

I'm not yet a JSON/AJAX master so I don't know how to do this.

 

I need a $_SESSION['name'] PHP variable to work with in my jQuery stuff and I don't know how to access it... consider:

 

// the 'who is typing' shindig
	$.ajax(
	{
		url: "whos_typing.html",
		cache: false,
		success: function(whos)
		{	
                         // here I need to access $_SESSION['name'] and do stuff with it

			$("#soandso").html(whos); //Insert who's typing into the #soandso div		
	  	}
	});

Link to comment
Share on other sites

You can do something like this, by mixing Javascript and PHP:

 

var name = <?php echo $_SESSION['name']; ?>;

Make sense? I think that is probably the easiest way. Another way might be to use use AJAX to retrieve the variable, but that seems like an unnecessary amount of work compared to simply echoing it out in the page.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...