www.webdeveloper.com
Results 1 to 3 of 3

Thread: Need to get form data from a page to store into a cookie, to reuse and add to.

Hybrid View

  1. #1
    Join Date
    Nov 2010
    Posts
    2

    Question Need to get form data from a page to store into a cookie, to reuse and add to.

    I need help, and I'm lost!

    I have a simple count-as-you click button form, and this is what I want to do with it:

    Click the button (however many times) to get the sets of numerical data. Take those numbers into a cookie, and be able to add/subtract to each number as i use the form again; and update the cookie. I need a way to be able to do this multiple if not an infinite number of times!

    Is this even possible?

    Help!

  2. #2
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    This is what I use to save a set of user options on a webpage delimited by the "|" symbol. You could have counter values instead of options and zero those counters if the cookie didn't exist.
    Code:
    function setCookie(c_name,value,expiredays)
    {
       var exdate=new Date();
       exdate.setDate(exdate.getDate()+expiredays);
       document.cookie=c_name+ "=" +escape(value)+
       ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
    }
    
    function getCookie(c_name)
    {
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
    }
    and
    Code:
     if (retCookie.length > 10) {  // Cookie was valid
        crumbs = retCookie.split("|");
        document.nwcform.NoteDur.selectedIndex = crumbs[0];
        document.nwcform.bIsDot.checked = crumbs[1];
        document.nwcform.A1[crumbs[2]].checked = true;
        document.nwcform.selAll.checked = crumbs[3];
        document.nwcform.FretQwikCh.checked = crumbs[4];
    ..... ; }
      else {  //No or invalid cookie
        document.nwcform.selAll.checked = true;
        document.nwcform.namCrdC.checked = true;
        document.nwcform.A1[1].checked = true; }
    
    var my_cookie = "";
      my_cookie += document.nwcform.NoteDur.selectedIndex + "|";
      my_cookie += ((document.nwcform.bIsDot.checked) ? 1 : 0 ) + "|";
      my_cookie += readStrum() + "|";
      my_cookie += ((document.nwcform.selAll.checked) ? 1 : 0 ) + "|";
    .....
      setCookie("WPGuiChord",my_cookie,91);  //Set for three months
    HTH

  3. #3
    Join Date
    Nov 2010
    Posts
    2
    Ok... looks good, i'll try it! Thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles