Create a cookie from form variable.

Discussion in 'JavaScript' started by andykzzzzzz, Dec 4, 2006.

  1. #1
    I have a form that corrects a quiz and assigns the number correct to a variable. This is the code:

    function scoretest(form) {
    score=0
    var page=null
    if (form.question1.value=="1") { score=score+1; }
    if (form.question2.value=="1") { score=score+1; }
    if (form.question3.value=="1") { score=score+1; }
    if (form.question4.value=="1") { score=score+1; }
    if (form.question5.value=="1") { score=score+1; }
    if (form.question6.value=="1") { score=score+1; }
    if (form.question7.value=="1") { score=score+1; }
    if (form.question8.value=="1") { score=score+1; }
    if (form.question9.value=="1") { score=score+1; }
    if (form.question10.value=="1") { score=score+1; }

    form.per.value = (score / 10)*100
    form.score.value = (score);

    document.cookie=score;


    THIS WORKS. It scores the quiz and creates a cookie. BUT it doesn't name the cookie. It just sends the cookie value. I need to name the cookie so that I can retrieve it later.

    Any help or ideas would be appreciated.
     
    andykzzzzzz, Dec 4, 2006 IP
  2. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    document.cookie =
      'cookiename=value; expires=Mon, 4 Dec 2006 12:00:00 UTC; path=/'
    
    Code (markup):

    fill in the cookiename with whatever you want to call it.
    set the value and put in an expire date too.
     
    lbalance, Dec 4, 2006 IP