Page 1 of 2

[SOLVED]Not sure if it is a question for a php forum...

Posted: Wed Sep 15, 2004 12:30 pm
by grammic
Ok here's the deal...
I use cookies in my site in javascript and not in php because I have asp pages. I can't turn them to php so that's out of the question.
When I get information from the cookies I need to pass it from a javascript variable to a php variable so I can store it in a database.
Is there any way to do it or am I waisting my time looking for one;

Thanks
Maria

Posted: Wed Sep 15, 2004 1:11 pm
by AVATAr
the way to talk with php from de client is using post or get methods, so you can do it from javascript

Posted: Thu Sep 16, 2004 3:08 am
by CoderGoblin
PHP is server based. Once it has been delivered to the client for javascript usage that is it, no more server side handling. Javascript is Client side. Once the page has been delivered to the client it can do it's magic. The only way for javascript to change PHP values is to deliver information back to the server. This is normally performed as mentioned through POST ot GET values. POST/GET via a form (possible hidden values) or sometimes GET via the javascript Location command.

Posted: Thu Sep 16, 2004 3:47 am
by patrikG
As far as I am aware (I simply have never heard anything to the contrary) cookies don't depend on the language they've been created with. So you should be able to read a cookie set with ASP with PHP, Javascript and vice versa.

Regarding server- and client-side: the lines are beginning to blur (See also: http://www.sitepoint.com/blog-post-view.php?id=192138 and http://www.sitepoint.com/blog-post-view.php?id=193677 )

Posted: Thu Sep 16, 2004 4:03 am
by grammic
patrikG you mean that if I set cookies with javascript I can get them with php;
The more I think of it the more it makes sense ...
I am going to try and see if it works...

Posted: Thu Sep 16, 2004 4:35 am
by patrikG
Great. Tell us what you've come up with.

Posted: Thu Sep 16, 2004 5:09 am
by grammic
I think I should be a little more descriptive because I've managed to confuse even myself!

I have .asp pages on my site with a lot of links. I want to keep track of the links a user follows so I use cookies, every time a link is chosen a cookie is set. I can do that only in javascript because it's the only client-side language I know and the scripts are recognized as javascript.

Now, the only thought I have was to use the onunload event to get the cookies. That means I can only use javascript to get the cookies. Then I have to find a way to pass the data to the php script that handles the insertion of it to the database. I can't use forms, I want it to be done quietly because I already have other forms, it would be to tiring for the users.

Is there any way to do it; Can I use something else besides the onunload event;
I kind of think it can't be done because I' m asking to much...

Posted: Thu Sep 16, 2004 7:09 am
by dethron
post a variable to the page itself, and then at the top of the page, get cookie, set cookie, insert something to db, then redirect the client to the destination.

Posted: Thu Sep 16, 2004 7:18 am
by grammic
Totally lost you...
Post a variable to the page itself; How; With a form;
And besides in an asp page I can't use php to insert data in the database.

Posted: Thu Sep 16, 2004 7:21 am
by dethron
you do not have to use "post" method, you can also use "get method" via link,
something like that

?carriage=1224584

and 1224584 implies the actual link.

at the top of your asp page, you can get this value and understand that user wants 1224584,
then increment its current stats, and redirect the user :)

if you can explain the scenario better, we can help better :)

Posted: Thu Sep 16, 2004 7:51 am
by grammic
Ok I'll try...

Let's say I have the following page which I save as .asp

Code: Select all

<html>

<head><title>test</title>

<SCRIPT src="cookie.js" type=text/javascript></SCRIPT>

<script>

var line1="course"
var line2="one"
var line3="two"
var line4="three"

</script>

</head>


<body onload=setCookie("course",line1);>

<!--#include file="something.asp"-->

<form method="post" name="do" action="do.php" target="_blank">
<input type="Hidden" name="math" >
<input type="submit" name="enter" value="submit" 
onClick="document.forms&#1111;'do'].math.value='go'">
</form>

<br><br><br><br>

<a href="http://www.bla.org/index.htm" onclick=setCookie("one",line2);>one</a>

<a href="http://www.bli.org/do.html" onclick=setCookie("two",line3);>two</a>

<a href="dada.doc" onclick=setCookie("three",line3);>three</a><br>


</body>
</html>
Now I have set four cookies. How can I get them on unload and then pass them to a php script;
I already use a form.
I don' t know if that makes more sense... :?

Posted: Thu Sep 16, 2004 7:59 am
by ol4pr0

Code: Select all

echo $_COOKIE['course'];

Code: Select all

document.write.getCookie(course); in javascript i blieve..

Posted: Thu Sep 16, 2004 8:07 am
by dethron

Code: Select all

<%
	'get varible named as id, since it is sent by href tag, you can use GET-method
	'set cookie
	'get  corresponding web address to this id from db
	'redirect user to the address that is obtained from db
%>

<html> 
<head><title>test</title> 
<SCRIPT src="cookie.js" type=text/javascript></SCRIPT> 
<script> 
var line1="course" 
var line2="one" 
var line3="two" 
var line4="three" 
</script> 
</head> 

<body onload=setCookie("course",line1);> 
<!--#include file="something.asp"--> 
<form method="post" name="do" action="do.php" target="_blank"> 
<input type="Hidden" name="math" > 
<input type="submit" name="enter" value="submit" 
onClick="document.forms&#1111;'do'].math.value='go'"> 
</form> 
<br><br><br><br> 
<a href="?id=1">one</a> 
<a href="?id=2">two</a> 
<a href="?id=3">three</a>
<br> 
</body> 
</html>

Posted: Thu Sep 16, 2004 8:16 am
by grammic
In order to use php code I have to do it in a .php page.
In that case the variable 'course' has to be passed to that page otherwise it's not recognized.
As for the second line it writes the value on the page;
Don't think I can use it...

Posted: Thu Sep 16, 2004 8:17 am
by dethron
there is no php-code, in my post !!!