1

I have a quiz in several HTML pages (I know it could be in only one but it has to be a page for question - q1, q2, q3...) with 4 radio buttons in each one (for q1: q1a, q1b, q1c, q1d; for q2: q2a, q2b, q2c, q2d, etc), and I want to set a cookie in each page with the name of the question (q1) and the checked answer (q1a, for example). I have created this function to set a cookie:

function set_cookie ( name, value ) {
    var cookie_string = name + "=" + escape ( value );  
    document.cookie = cookie_string;
}

And I have put it in each page of the quiz. It works fine in each page, but when in the final page I use a function to get the cookies, it returns null. The function I have is this one:

function get_cookie ( cookie_name ) {
    var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
    if ( results )
        return ( unescape ( results[2] ) ) ;
    else
        return null;
}

But when I call this function with the function to make the final grade of the quiz, it returns null:

function grade () {
    var grade = 0;
    var x = get_cookie( "q1" );
    var y = get_cookie( "q2" );
    alert(x); //Just to see if the function works fine
    ...
    var xx = 0;
    var yy = 0;
    ...
    if(x == "q1a") xx = 20; //correct answer
    else xx = 0; //wrong answers

    if(y == "q2c") yy = 20; //correct answer
    else yy = 0; //wrong answers
    ...
    ...
    grade = xx + yy;
    alert("Your qualification is "+grade);
}

What am I doing wrong? Is it not possible to get the cookies from one page in another one if both pages are in the same directory?

cr0ss
  • 877
  • 5
  • 20
Hec46
  • 181
  • 2
  • 15
  • I can't tell if its the cause of your immediate problem, but your application design will fail at some point due the limit on the numer of cookies: http://stackoverflow.com/questions/5381526/what-are-the-current-cookie-limits-in-modern-browsers – symcbean Apr 24 '14 at 11:11
  • I don't know where is the error it should works, probably is something in the regular expression or in the escape/result[2]. You should debug the code and see where the cookie lose it's value. i suggest to look at this small script to control and create the cookie. Look if it can help you to find a solutioin https://developer.mozilla.org/en-US/docs/Web/API/document.cookie – Emanuele Parisio Apr 24 '14 at 13:11

1 Answers1

0

The best way would be to use local storage not cookies. Check w 3 schools.

It is really simple to achieve and it can be carried throughout the pages

I hope that helps!

KM123
  • 1,339
  • 1
  • 10
  • 21
  • I have been searching about local Storage and it seems just what I need, but I don't understand how to get the values back. I found that it is done with the line: `document.getElementById("result").innerHTML=localStorage.q2;` but I don't know what to do with it. Can it be assigned to a variable? It says that it is assigned to the element with id="result", but which one is it and how do I use it inside the function? – Hec46 Apr 24 '14 at 20:38
  • Okay so to get the value and to assign it to a variable name you use: var variable1 = localstorage.getitem('q2'); – KM123 Apr 24 '14 at 20:52
  • So then you can use the variable as you want! :) – KM123 Apr 24 '14 at 20:53
  • Finally! It works perfectly! Now I see that I was complicating myself with the cookies when this is just easier and better! Thanks mate! :) – Hec46 Apr 24 '14 at 21:33
  • 1
    Im pleased you have sorted it and no problem :) – KM123 Apr 25 '14 at 06:41