473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

May 14 '06 #1
4 7942
Rik
Bart wrote:
<script type='text/javascript'>
<!--
document.cookie = "qrv=''";
alert(document.cookie);
//-->
</script> 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.


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
May 14 '06 #2
Bart wrote:
(..)
Problem is that I always have to refresh the webpage in order to get
the cookie value.
(..)
Any ideas how to fix that?


Sounds like you need AJAX!
May 14 '06 #3
Thanks I will try other options.

May 15 '06 #4
is there a easy way to refresh a page.

May 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vince C. | last post by:
Hi all. I'm trying to set a cookie expiry date but my script is JS (JavaScript). I've tried Response.Cookies("Test").Expires = Date(); Response.Cookies("Test").Expires =...
7
by: Bert | last post by:
I have been reading the post and the FAQ and have been unable to find anything that will help with my problem. First let me say that I am not a web developer, designer and no next to nothing...
2
by: Arpan | last post by:
In my ASP application, a Session variable takes the following form <% Dim strRec,strCol strRec=Request.QueryString("records") strCol=Request.QueryString("columns") Session("RecCol")=strCol &...
9
by: SHarris | last post by:
Hello, In our new intranet ASP.NET project, two requirements are that the browser accept cookies AND JavaScript. We are requiring the use of Internet Explorer 6+. 1. Using C# in an ASP.NET...
2
by: HopfZ | last post by:
Server sends cookie to browser and the browser send the same cookie back to the server according to Wikipedia. Do browsers send even javascript-generated cookie to servers? For example, if I...
3
by: WayneH | last post by:
Hi - I'm trying to use javascript to determine if a user's browser has cookies enabled or not. To test: copy this code into a file with a 'html' extension, and load it into your IE browser...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.