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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

grammic
Forum Newbie
Posts: 19
Joined: Wed Sep 15, 2004 12:27 pm

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

Post 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
Last edited by grammic on Fri Sep 17, 2004 6:56 am, edited 1 time in total.
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

the way to talk with php from de client is using post or get methods, so you can do it from javascript
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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 )
grammic
Forum Newbie
Posts: 19
Joined: Wed Sep 15, 2004 12:27 pm

Post 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...
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Great. Tell us what you've come up with.
grammic
Forum Newbie
Posts: 19
Joined: Wed Sep 15, 2004 12:27 pm

Post 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...
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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.
grammic
Forum Newbie
Posts: 19
Joined: Wed Sep 15, 2004 12:27 pm

Post 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.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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 :)
grammic
Forum Newbie
Posts: 19
Joined: Wed Sep 15, 2004 12:27 pm

Post 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... :?
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

echo $_COOKIE['course'];

Code: Select all

document.write.getCookie(course); in javascript i blieve..
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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>
grammic
Forum Newbie
Posts: 19
Joined: Wed Sep 15, 2004 12:27 pm

Post 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...
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

there is no php-code, in my post !!!
Post Reply