Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 14th, 2006, 09:45 PM
Bart
Guest
 
Posts: n/a
Default passing javascript cookies into php script

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

  #2  
Old May 14th, 2006, 10:35 PM
Rik
Guest
 
Posts: n/a
Default 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


  #3  
Old May 14th, 2006, 11:05 PM
Henk Verhoeven
Guest
 
Posts: n/a
Default 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!
  #4  
Old May 15th, 2006, 02:35 AM
Bart
Guest
 
Posts: n/a
Default Re: passing javascript cookies into php script

Thanks I will try other options.

  #5  
Old May 15th, 2006, 02:35 AM
Bart
Guest
 
Posts: n/a
Default Re: passing javascript cookies into php script

is there a easy way to refresh a page.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.