Log in

View Full Version : PHP is not reading cookie until I refresh - help



sic00
05-20-2009, 09:49 PM
I am having the following problem. On a php script I am setting a cookie with javascript and a few lines lower I am trying to recall the value in php. The php is not getting the value unless I refresh the page. Can you please help me find a solution?
My code is as follows...



<SCRIPT LANGUAGE=JavaScript>
hash = parent.location.hash;
ar=hash.substring(1);
document.cookie = "video="+ar;
</SCRIPT>

<?php
$ar = $_COOKIE["video"];
echo "ar-->".$ar;
?>



the reason I am doing this is because I dont know some other way to pass a variable from javascript to php (on the same script).
I want to read a variable from the url not using GET or POST but using the hash (#) value.
I am doing this because I want to change the URL from a click on a flash object without refreshing the page.

Nile
05-20-2009, 10:01 PM
I think it should be:


document.cookie = "video = "+escape(ar);


That should work.

(This works for me):


<script type="text/javascript">
document.cookie = "greeting =" +escape("Hello");
</script>
<?php echo $_COOKIE['greeting']; ?>

sic00
05-20-2009, 10:06 PM
thank you for your reply but unfortunatelly its not working.
I even made a test file with your script..


<script type="text/javascript">
document.cookie = "greeting =" +escape("Hello");
</script>
<?php echo $_COOKIE['greeting']; ?>

but its not working either.
It only works after I refresh.

sic00
05-20-2009, 10:10 PM
I 've read the following in the PHP manual:
Cookies will not become visible until the next loading of a page that the cookie should be visible for.
so.. I am afraid that its not possible to do what I want to do.
is there any other way to pass the variable value from javascript to php?

Nile
05-21-2009, 11:52 AM
You can do something like this:


<script type="text/javascript">
var a = "food";
</script>
<?php
$a = "<script type='text/javascript'>document.write(a);</script>";

echo $a;
?>

Or read about Ajax (search on google).

sic00
05-22-2009, 01:08 PM
thank you very much Nile.
thats what i was looking for...
$a = "<script type='text/javascript'>document.write(a);</script>";

it was very simple but i am not an experienced coder and i didnt know it was possible.. thank you..
although i think i should learn some things about Ajax...

I am making the following site www.ecogreens.tv (http://www.ecogreens.tv)
and the reason i want to do this is that i want the left side of the page (the video player) to refresh when I click on a link on the right side (the swf with the categories and video list) but i want the swf to stay as it is.

sic00
05-22-2009, 01:14 PM
Nile... another problem came up... :)
i am using your method and the variable is being echoed fine.
but when i am using it on a sql query a few lines lower i am getting an error..
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL...


$ar = "<script type='text/javascript'>document.write(ar);</script>";
//$ar = '124356';
echo "ar-->".$ar;
$result = mysql_query ("SELECT thumb,title,descr FROM videos WHERE url='$ar' LIMIT 1");
while($row = mysql_fetch_array($result))

the echo works fine but i get the error on the while line.
if i use the $ar = '124356'; things are working so there is no some other error...
could you explain this?

Nile
05-22-2009, 10:10 PM
This is because it queries like this:


SELECT ... WHERE url='<script type='text/javascript'>document.write(ar);</script>'

You need to look up ajax online for this to work.

djr33
05-23-2009, 03:42 AM
You are approaching this backwards: PHP loads ENTIRELY and only after that will the javascript process. PHP is a generation program for text (in this case html/javascript), that is only after being generated passed to the browser. Then the browser can do what it wants. There is no possible way to run Javascript on a page before that page is parsed with PHP. Using Ajax will get you a way to send a variable back to the PHP AFTER it is parsed and save you having to refresh the page, but not before you load the page the first time.