Click to See Complete Forum and Search --> : cookie printed in form


Anne Vance
09-29-2003, 09:23 AM
In the following code I can make the cookie popup in an alert, (this is only to check if the cookie is OK).
I want the value of the cookie printed in the form field NAME="invoer" but I get the code only.
Where do I go wrong?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
function read_cookie()
{
alert (document.cookie);
}
// -->
</script>
</head>

<body>

<FORM NAME="Formulier">
Is dit de url? <INPUT TYPE="Text" VALUE=<?php $COOKIE["document.cookie"] ?> NAME="invoer">
<p>

<INPUT TYPE="BUTTON" VALUE="Cookie lezen"
onClick="read_cookie()">
</FORM>

</body>
</html>

Khalid Ali
09-29-2003, 09:30 AM
VALUE=<?php $COOKIE["document.cookie"] ?>

are you running php??

if yes make sure your pages extension is not html it should be php ...etc

o35_jimmy
09-29-2003, 09:34 AM
try put dblquote
VALUE="<?php $COOKIE["document.cookie"] ?>"

Anne Vance
09-29-2003, 09:56 AM
yes I'm running php and changed the pagename to .php
Also followed Jimmy's advoce but now there is a blank form field, while the "test" button still works.
Any other idea's

pyro
09-29-2003, 10:06 AM
A few thing that I notice with that:

First of all, you are not echoing the cookie out and $COOKIE should be $_COOKIE (or $HTTP_COOKIE_VARS in old versions of PHP), you need to use:

<input type="text" value=<?php echo $_COOKIE["document.cookie"]; ?> name="invoer">

Next, that assumes your cookie is named document.cookie. Unlikely.

Try setting the document.cookie part to the name of the cookie:

<input type="text" value=<?php echo $_COOKIE["cookiename"]; ?> name="invoer">

Anne Vance
09-29-2003, 11:44 AM
Still doesn't work
when I have this:
<FORM NAME="Formulier">
Is dit de url? <INPUT TYPE="Text" VALUE=<?php echo $_COOKIE["document.cookie"]; ?> NAME="invoer">

I get Name="invoer"

and when I use

<FORM NAME="Formulier">
Is dit de url? <INPUT TYPE="Text/javascript" VALUE="document.cookie" NAME="invoer">

I get document.cookie


The idea is a photoalbum, where you can choose a photo, and when you press a button the url of that photo is printed on another page in a field of a form where the buyer can fill out the rest and email, so I know which foto he wants.

pyro
09-29-2003, 11:59 AM
If you show me how you set the cookie, I will show you the correct way to display it with PHP. I'm still quite sure you have the cookie name wrong...

Anne Vance
09-29-2003, 12:07 PM
This is on the page with the photo's

<SCRIPT LANGUAGE="JavaScript">
<!--
function write_cookie()
{
var verval =1000*60*60*24;
var nu = new Date();
var vervaltijd = new Date(nu.getTime() +
verval);
vervaltijd = vervaltijd.toGMTString() ;
document.cookie =document.location +
"; expires=" + vervaltijd + ";" ;
}
// -->
</script>

btw I just got a solution which works:

<form NAME="Formulier">
<p>Is dit de url?
<script language="javascript">
document.write('<input TYPE="Text" VALUE="'+document.cookie+'" NAME="invoer" size="80">')
</script>

Do you agree?
Thanks

pyro
09-29-2003, 12:12 PM
Yes, you could use that, since you are setting the cookie with JavaScript anyway, and it won't work for those with JavaScript disabled.

For future reference, though, if you wanted to write it out with PHP, you'd want to set a name on your cookie, like this:

<script type="text/javascript">
<!--
function write_cookie()
{
var verval =1000*60*60*24;
var nu = new Date();
var vervaltijd = new Date(nu.getTime() +
verval);
vervaltijd = vervaltijd.toGMTString() ;
document.cookie ="cookiename="+document.location +
"; expires=" + vervaltijd + ";" ;
}
// -->
</script>Then, you can read it with this PHP

$_COOKIE['cookiename'], so in your previous example, we'd want:

<input type="text" value=<?php echo $_COOKIE['cookiename']; ?> name="invoer">