Connecting Tech Pros Worldwide Help | Site Map

passing javascript cookies into php script

Bart
Guest
 
Posts: n/a
#1: May 14 '06
Hello,

I created a php script in which I call php fumcion which looks like
this

function confirm($vl) {


?>
<head>
<script type='text/javascript'>
<!--
document.cookie = "qrv=''";
alert(document.cookie);

var answer = confirm("ARE YOU SURE?")



if (answer){
alert('ok');
document.cookie = "qrv=yes";
alert(document.cookie);

}
else{
alert('no');
document.cookie = "qrv=no";
alert(document.cookie);

}

//-->
</script>
</head>
<?
}


Later after I call the above function I want to check what is the
session value

echo $_COOKIE["qrv"];

it is null, but the actual cookie in the browser is set to yes or no.
When I try again the echo command give me the right output.

Problem is that I always have to refresh the webpage in order to get
the cookie value.
It looks like php reads the cookie that was set before you load the
browser page.



Any ideas how to fix that?

Thank You

Bartosz Wegrzyn

Rik
Guest
 
Posts: n/a
#2: May 14 '06

re: passing javascript cookies into php script


Bart wrote:[color=blue]
> <script type='text/javascript'>
> <!--
> document.cookie = "qrv=''";
> alert(document.cookie);
> //-->
> </script>[/color]
[color=blue]
> Later after I call the above function I want to check what is the
> session value
>
> echo $_COOKIE["qrv"];
>
> it is null, but the actual cookie in the browser is set to yes or no.
> When I try again the echo command give me the right output.
>
> Problem is that I always have to refresh the webpage in order to get
> the cookie value.
> It looks like php reads the cookie that was set before you load the
> browser page.[/color]

It's the old story:
Javascript runs on the users computer, PHP on the server. PHP sends output
"as is" to the client, with the variables available.
Because the cookie is set AFTER the user has gotten the page, at the time
the page was set this cookie variable wasn't available. No way to "fix"
that, that's just how it works. If you need this functionality, it needs a
different approach. Either create the dynamic content using javascript (for
instance XMLHTTPRequest), or let the user go to a different page.

Grtz,
--
Rik Wasmus


Henk Verhoeven
Guest
 
Posts: n/a
#3: May 14 '06

re: passing javascript cookies into php script


Bart wrote:[color=blue]
> (..)
> Problem is that I always have to refresh the webpage in order to get
> the cookie value.
>(..)
> Any ideas how to fix that?[/color]

Sounds like you need AJAX!
Bart
Guest
 
Posts: n/a
#4: May 15 '06

re: passing javascript cookies into php script


Thanks I will try other options.

Bart
Guest
 
Posts: n/a
#5: May 15 '06

re: passing javascript cookies into php script


is there a easy way to refresh a page.

Closed Thread