Web Hosting Talk







View Full Version : Can't read cookie set in Javascript from PHP


shiferaw
10-27-2002, 03:39 PM
Why can't I read a cookie I set in Javascript read in my PHP script? Is this not possible? I know the cookie is being set because I can find it on my computer. The problem is that PHP does not see it.

Here is javascript code to set the cookie:

<script language="JavaScript">
function CurrentCity(dropdown)
{
var myindex = dropdown.selectedIndex
var SelValue = dropdown.options[myindex].value
document.cookie = 'yyy = ' + SelValue + ' ; expires=Fri, 31 Dec 2002 23:59:59 GMT; path=/ ;';
}
</script>


Here is PHP code to read the cookie

if (empty($city)) {
print "not found"
} else {
print $city;
}

Thank You!

shiferaw
10-27-2002, 03:41 PM
Correction:

Please read:
"document.cookie = 'yyy " as
document.cookie = 'city...."

michaeln
10-27-2002, 04:42 PM
Try this:


if (empty($_COOKIE["city"]))
{
print "not found"
}
else
{
print $_COOKIE["city"];
}

shiferaw
10-27-2002, 04:52 PM
Thank you but it still did not work. any cules. This is a puzzel for me.

michaeln
10-27-2002, 04:59 PM
If SelValue is equal to zero then the script will still print Not Found. Don't know if that is the problem or not. Other than that it should work fine...

I.E. http://hostevolve.com/tmp/cookie.php

The first time you run it you will see cookie not set on the screen. Each time after you will see "Computer"....

Rich2k
10-28-2002, 06:44 AM
Are you by any chance trying to read the cookie on the SAME page as you set it in JavaScript? As you would need to call a new script before you could read it.

michaeln
10-28-2002, 09:16 AM
Yeah I suppose I should have mentioned that. You have to reload the page for PHP to be able to read the cookie...