Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What would be the best approach to this?

Because, as I found (and it made total sense, only after having tried it :p) that you can't set a PHP variable on javascripts conditions. (duurrhh)

The only solution I can come up with is to do an AJAX call to a small PHP file that handles the session variables

elm.click(function() {
   $.post("session.php", { "hints":"off" });
   turnOffHints();
}

And then let the session.php deal with setting the new variable.

But it sorta feels like a waste to do a full http request only to set one variable in PHP...

share|improve this question

2 Answers

up vote 6 down vote accepted

Javascript (jQuery) is executed on the client-side.

PHP, and its session mecanism, is executed on the server-side.

So, to do anything related to a PHP session, you have to go to the server, doing a full HTTP Request -- you cannot stay on the client-side.

Doing an Ajax request is a way of going to the server -- works fine ;-)
What you are doing is OK (and necessary, actually), if you want to affect some session data : you don't have much of a choice, even if it feels like some kind of waste.

share|improve this answer
that's what I feared. It wasn't really a hassle, though, so I guess it'll be okay... – peirix Oct 7 '09 at 11:22

The only other alternative is to make a "regular request" but that would be even more "wasteful".

Or maybe you could set a cookie with JavaScript and change the session when the next request comes in (you would have to check for this cookie on every request then) ...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.