Newbie - simple cookie for form

R

Robin Goodfellow

Hi,

I was wondering if someone could please help me with a very basic cookie. I
have a feedback form with name and email address that are in 2 text boxes in
a form, I want the cookie to just remember the details and load them the
next time the person comes back to the page. I have been looking now for a
while but the examples the I find are all very complex, with expiry dates
etc.

Any help would be much appreciated
 
M

Michael Winter

on 13/11/2003:
Hi,

I was wondering if someone could please help me with a very basic cookie. I
have a feedback form with name and email address that are in 2 text boxes in
a form, I want the cookie to just remember the details and load them the
next time the person comes back to the page. I have been looking now for a
while but the examples the I find are all very complex, with expiry dates
etc.

If you don't specify an expiry date, the cookie will be deleted as
soon as the session ends. I forget exactly what ending a session is
defined as: either leaving the site (changing domain), going to a
higher path, changing page, or closing the browser. Could someone
else clear that point up, please?

The way to avoid any fuss, is to just set the cookie to expire a fixed
time after the current time, say 24 hours. To get that date and time,
you could do something like:

// new Date() gives the current time
// valueOf() converts that time to milliseconds
// 86400000 is the number of milliseconds in a day
expiry = new Date(( new Date() ).valueOf() + 86400000 );

You can then append it to your cookie values like so:

// Your supposed to use a GMT string, but toGMTString()
// is depreciated in JS v1.3 onwards and just calls toUTCString()
// UTC is fine, anyway (that is, it works)
// myCookie should contain your name/value pair
myCookie += ';expires=' + expiry.toUTCString();

To set your values, do something like this:

document.cookie = 'email=' + escape( email ) + ';expires=' +
expiry.toUTCString();

You can only set one cookie per assignment (though I'm sure I did more
than one once - must be my imagination), but you can assign to the
'cookie' property many times. When you read the cookie back, all
name/value pairs will be concatenated into one long string, separated
by semi-colons.

To remove a cookie, assign the cookie name with an expiry date before
the current time. You could do something like this:

// changed to generate date one day ago
expired = new Date(( new Date() ).valueOf() - 86400000 );
// remove the email value - any others will remain
document.cookie = 'email=;expires=' + expired.toUTCString();

Can someone answer me this: if different expiry dates are used on
subsequent assignments, is the last one used for all earlier cookies
in that document?

Mike
 
R

Robin Goodfellow

I got this sort of working, the only problem I know have is that the text
boxes in the form load with the word "Undefined" in them, I need them to be
blank. Can anyone help me?

var form_name = "";
var form_email = "";

if (document.cookie && document.cookie != ""){
var old_cookie_data = unescape(document.cookie);
var old_split = old_cookie_data.split(";");

for (i = 0; i < old_split.length; i++){
if (old_split.indexOf("name_cookie") > -1){cookie_name = old_split;}

if(old_split.indexOf("email_cookie") > -1){cookie_email = old_split;}
}
var old_cookie_name_split = cookie_name.split("=");
var form_name = old_cookie_name_split[1];
var old_cookie_address_split = cookie_email.split("=");
var form_email = old_cookie_address_split[1];
}

function set_cookies()
{
client_name = document.form.realname.value;
client_email = document.form.FromEmail.value;

document.cookie = "name_cookie=" + escape(client_name);
document.cookie = "email_cookie=" + escape(client_email);

}

function autofill()
{
document.form.realname.value=form_name;
document.form.FromEmail.value=form_email;
}
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
The way to avoid any fuss, is to just set the cookie to expire a fixed
time after the current time, say 24 hours. To get that date and time,
you could do something like:

// new Date() gives the current time
// valueOf() converts that time to milliseconds
// 86400000 is the number of milliseconds in a day
expiry = new Date(( new Date() ).valueOf() + 86400000 );

That contains a false comment, but gives an answer agreeing with the
text. However, it "wastes" a Date Object.

with (expiry = new Date()) setHours(getHours()+24)

is shorter, and uses the units called for.

// changed to generate date one day ago
expired = new Date(( new Date() ).valueOf() - 86400000 );

No, that can give the wrong date.

with (expired = new Date()) setDate(getDate()-1)
 
M

Michael Winter

on 13/11/2003:
JRS: In article <[email protected]>, seen
in news:comp.lang.javascript, Michael Winter <M.Winter@[no-spam]> posted
at Thu, 13 Nov 2003 02:25:51 :-
The way to avoid any fuss, is to just set the cookie to expire a fixed
time after the current time, say 24 hours. To get that date and time,
you could do something like:

// new Date() gives the current time
// valueOf() converts that time to milliseconds
// 86400000 is the number of milliseconds in a day
expiry = new Date(( new Date() ).valueOf() + 86400000 );

That contains a false comment, but gives an answer agreeing with the
text. However, it "wastes" a Date Object.

Which line is false?

new Date() returns a Date object initialised to the current date and
time. I know of no other method of returning this information.

Date.valueOf() returns the primitive value of the Date object: the
number of milliseconds that have elapsed since 01-Jan-1970 00:00:00
UTC.

86400000 is the number of milliseconds in a day (ECMA-262, section
15.9.1.1)

The comments in my original post were more terse than those above, but
are essentially correct. I could have been more precise, but I did
not want to resort to counting line lengths to avoid wrapping by my
newsreader.
with (expiry = new Date()) setHours(getHours()+24)

is shorter, and uses the units called for.


No, that can give the wrong date.

In what cases? Moreover, it doesn't matter what date the operation
returns. The point is to create a Date that is before the present: 24
hours earlier is a safe way of accomplishing that.
with (expired = new Date()) setDate(getDate()-1)

I presented the date manipulations in that manner because I was under
the impression that setHours() should take values that are within
normal limits (0 <= hours < 24, 0 <= mins < 60, 0 <= secs < 60, 0 <=
ms < 999) These are listed in Netscape's JavaScript reference.
However, I did not see in the description that attempts will be made
to roll-over values outside of those limits.

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
JRS: In article <[email protected]>, seen
in news:comp.lang.javascript, Michael Winter <M.Winter@[no-spam]> posted
at Thu, 13 Nov 2003 02:25:51 :-
// 86400000 is the number of milliseconds in a day
That contains a false comment,
Which line is false?

That one; but on average it is right.
Date.valueOf() returns the primitive value of the Date object: the
number of milliseconds that have elapsed since 01-Jan-1970 00:00:00
UTC.

So it says. Actually, it means GMT or UT, since Leap Seconds are
ignored; but that does not affect the matter in hand.

86400000 is the number of milliseconds in a day (ECMA-262, section
15.9.1.1)

At this stage of section 15.9, we are still using Real Time; GMT, in
effect. We have not reached a 15.9.1.9 (my copy of ECMA has 2 of
those). ECMA is correct here, although it could be expressed better.

Recall what you may have done about 19 days ago; in principle at 01:00
GMT on Sunday 26th.


In what cases?

When called in the EU on Mon 2003-03-31 but before 01:00 local time; at
the same local time a week later in much of North America; at the
corresponding spring date in many other places, including NZ and much of
AU; and similarly in other years.

Also, differently, for EU/US Sun 2003-10-26 after 23:00 local, etc.
Moreover, it doesn't matter what date the operation
returns.

The comment says "date one day ago", and the code does not always
comply. If the comment has said 24 hours ago, that would have been
different.
The point is to create a Date that is before the present: 24
hours earlier is a safe way of accomplishing that.

It is not.
I presented the date manipulations in that manner because I was under
the impression that setHours() should take values that are within
normal limits (0 <= hours < 24, 0 <= mins < 60, 0 <= secs < 60, 0 <=
ms < 999) These are listed in Netscape's JavaScript reference.
However, I did not see in the description that attempts will be made
to roll-over values outside of those limits.

QUOTE of NS, page Last Updated: 05/28/99 11:59:09 :
If a parameter you specify is outside of the expected range, setHours
attempts to update the date information in the Date object accordingly.
For example, if you use 100 for secondsValue, the minutes will be
incremented by 1 (min + 1), and 40 will be used for seconds.

NS guide "Last Updated September 28, 2000" is an inferior work,
probably produced by a PR department rather than a programmer.

All the set* methods have similar statements, apart from setYear
(inapplicable), setTime (ditto), setDate (seemingly an accidental
omission); likewise Date.UTC.


Note how ECMA MakeDate uses MakeDay & MakeTime; and that their
parameters must be finite, but need be no smaller.


Rollover of date-time fields has been used quite frequently in this
newsgroup for some years; perhaps you arrived since it was last
mentioned, and have not taken into account what the FAQ says about Date?



BTW, a simpler way to get an expiry Date Object set before the present
moment is new Date(0).


When referring to different expiry dates: it is not clear whether your
"last" means the one most recently given or the one latest in value.
 
M

Michael Winter

on 14/11/2003:

Sorry, I should have phrased that better: I did not see the
description of roll-overs until after I sent my reply.
Rollover of date-time fields has been used quite frequently in this
newsgroup for some years; perhaps you arrived since it was last
mentioned, and have not taken into account what the FAQ says about
Date?

I am new to this newsgroup, and I have read the FAQ. Nothing in
particular is mentioned about dates, so I take it you mean the
description of Date usage on your web site?
BTW, a simpler way to get an expiry Date Object set before the present
moment is new Date(0).

Good point.
When referring to different expiry dates: it is not clear whether your
"last" means the one most recently given or the one latest in value.

I meant the most recently applied expiry date.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top