Newbie - simple cookie for form

Discussion in 'Javascript' started by Robin Goodfellow, Nov 13, 2003.

  1. 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
     
    Robin Goodfellow, Nov 13, 2003
    #1
    1. Advertising

  2. "Robin Goodfellow" wrote 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

    --
    Michael Winter
    M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
     
    Michael Winter, Nov 13, 2003
    #2
    1. Advertising

  3. 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;
    }
     
    Robin Goodfellow, Nov 13, 2003
    #3
  4. Robin,
    Try some code from here. This does what you are talking about:

    http://ccl.flsh.usherbrooke.ca/eslcafe/million.htm

    It's all in the setcookie() and getcookie() fucntions.

    http://ccl.flsh.usherbrooke.ca/eslcafe/

    Without seeing what you are doing, I know this works. Because it is all client-side you can even set your cookie at the top of the page and read it further down on the same page.



    > 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.
     
    John Taylor-Johnston, Nov 13, 2003
    #4
  5. JRS: In article <PYBsb.2447$>, 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.

    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)

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
     
    Dr John Stockton, Nov 13, 2003
    #5
  6. Thanks for your help
     
    Robin Goodfellow, Nov 13, 2003
    #6
  7. "Dr John Stockton" wrote on 13/11/2003:

    > JRS: In article <PYBsb.2447$>,

    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.
    >
    > >// changed to generate date one day ago
    > >expired = new Date(( new Date() ).valueOf() - 86400000 );

    >
    > 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

    --
    Michael Winter
    M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
     
    Michael Winter, Nov 13, 2003
    #7
  8. JRS: In article <4vRsb.3236$>, seen
    in news:comp.lang.javascript, Michael Winter <M.Winter@[no-spam]> posted
    at Thu, 13 Nov 2003 20:06:24 :-
    >"Dr John Stockton" wrote on 13/11/2003:
    >> JRS: In article <PYBsb.2447$>,

    >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.



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

    >>
    >> No, that can give the wrong date.

    >
    >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.

    >> 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.


    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.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
     
    Dr John Stockton, Nov 14, 2003
    #8
  9. "Dr John Stockton" wrote on 14/11/2003:

    > Michael Winter wrote:
    > >However, I did not see in the description that attempts will be

    made
    > >to roll-over values outside of those limits.


    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

    --
    Michael Winter
    M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
     
    Michael Winter, Nov 14, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    5,931
    Steven Cheng[MSFT]
    Jun 3, 2004
  2. Shapper

    Cookie and Session Cookie Questions.

    Shapper, Apr 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    607
  3. =?Utf-8?B?UGF1bA==?=

    Cookie Question (IP as domain and cookie file location)

    =?Utf-8?B?UGF1bA==?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,534
    Bruce Barker
    Jan 10, 2006
  4. Jimbo
    Replies:
    1
    Views:
    698
    Pierre Quentel
    Apr 8, 2010
  5. Replies:
    1
    Views:
    753
    Randell D.
    Mar 17, 2005
Loading...

Share This Page