Did you ever tried to access a JavaScript variable from your PHP code?
will, I did try, like everyone I just tried googling for the best solution, and the best way I found was to refresh the page once visited sending the variable within the URL query, and then get the variable using $_GET function in your PHP code.
but that didn't work in my case, because my variable were supposed to be sent, without the user knowing these variables or there values, and that would show it in the URL.
Even worst, it would be saved in his History, and everybody else could see the values.
so I thought of another way, and it worked as a charm.
although the Browser should be accepting cookies... (but comeon when was the last time you saw a browser configured NOT to accept cookies).
One More Thing. This won't work for users using FireFox or Safari.
Only IE users fall for that, unless changes to the default IE configuration were done.
the method goes as follow:
a. JavaScript creates a new cookie, and save the variables I need in that cookie.
b. PHP then can access that cookie and read the variables.
c. PHP re-set a cookie with the same name with 0 seconds expiry time, and no values to overwrite the old cookie.
Here is a working example tried on :
Apache 2.0.58 , PHP 5.1.4 , Internet Explorer 6.0.x
.... CODE START ....
<HTML>
<Script Language="JavaScript">
var content = "foo"
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
setCookie("CN",content,1);
</Script>
<?php
if (isset ($_COOKIE['CN']))
echo $_COOKIE['CN'] ;
else
echo 'cookie not set';
?> </HTML>