Click to See Complete Forum and Search --> : session variables


Salam
09-29-2003, 05:49 AM
Hi, I am using coockies for session variables to be shared between my javascript and php code. Problem is that this will not work for users who have coockies disabled. Is there another way that will work for all users? I am not interested in persistant coockies but just a means of having variables for the duration of the session ?

Thank you,
Salam

pyro
09-29-2003, 07:05 AM
The two other ways to pass variables from JavaScript to PHP are by submitting a form (and having set the form fields with JavaScript) and reloading the page and setting the query string. You are, however, not going to find as many users with cookies disabled, as JavaScript disabled (13% (http://www.thecounter.com/stats/2003/May/javas.php)), I think...

Håkan Englund
09-29-2003, 07:32 AM
Send the variable in the HTML as a <INPUT TYPE=HIDDEN NAME="xx" value="your data"> and let JavaScript read it.

Khalid Ali
09-29-2003, 08:51 AM
Just use PHP session to keep data alive from page to page,and what you will need to do is( as pyro said) submit the form to itself(leaving the action="" empty will do that.).
BTW..if javascript is disabled,cookies should automatically be disabled(don't ya think???):D

Salam
09-30-2003, 04:01 AM
Hi, I read your respoces carefully but not sure how much I understood of them. I have a need to understand because I use PHP, HTML and javascript a lot and need to close this gap in my understanding.

The two other ways to pass variables from JavaScript to PHP are by submitting a form (and having set the form fields with JavaScript) and reloading the page and setting the query string I understand the first method but do not understand what you ment with "and reloading the page and setting the query string".

Send the variable in the HTML as a <INPUT TYPE=HIDDEN NAME="xx" value="your data"> and let JavaScript read it I guess you mean this is how I can pass a value from the server PHP to the client javascript as an alternative to using coockies.

Just use PHP session to keep data alive from page to page,and what you will need to do is( as pyro said) submit the form to itself(leaving the action="" empty will do that.). First, I do not understand why submit the form to itself. on the other hand, I just finished reading about PHP Session Management. What I read states that when a session is initialized, a coockie of the session ID is set. What if the client has coockies disabled, can a coockie of the session ID be set and if not I guess all of the PHP Sesstion Management will fail !!

Thank you all
Salam

pyro
09-30-2003, 07:55 AM
Ok, take a look at this example: When uses click the button, the JavaScript variable will be passed into a PHP variable:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function setVar() {
//set a variable
myvar = "variable";
//reload the page with query string attached. equivilant to: page.php?jsvar=variable
window.location.href = window.location.href+"?jsvar="+myvar;
}
</script>
</head>
<body>
<button type="button" onclick="setVar();">Send to PHP</button>
<?PHP
if (isset($_GET['jsvar'])) {
#set $myvar to the value of the $_GET variable "jsvar"
$myvar = $_GET['jsvar'];
#echo the variable
echo $myvar;
}
?>
</body>
</html>