Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Setting and reading cookies?

    Hi there im new to the forums and i'm making a javascript game.

    Im having trouble setting and reading cookies.

    Ok to begin the user has 6 multiple choice questions and once they have completed the quiz a cookie will be set with their current score.

    When they reset the quiz and take the quiz again i want to read the cookie with their previous score and compare it to their new score and alert them to whether they have done better or worse second time around.

    Any help would be great.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    why don't you show us what you have tried.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here it is guys any help would be great...

    When you click the get score button i want to name the cookie and capture the name of the userand their score.

    Store this in a cookie.

    The user then resets the quiz and takes it again this time when they submit get their score they get their current score then alert boxes telling them that they have done better or worse than the last time.

    Thanks very much guys really appreciate the help












    <HEAD>

    <style type="text/css">
    <!--
    .bgclr {background-color: white; color: black; font-weight: bold;}
    -->
    </style>

    <script language="JavaScript">
    //Welcome Alert
    alert("Welcome to the Breeds of Dogs Quiz");

    //Asks User for name to be retained for feedback later
    var username = prompt("Please Enter Your Name","");
    if(username =="") {
    username ="Dog Lover";
    }
    alert("Thanks " + username);


    // Insert number of questions
    var numQues = 6;

    // Insert number of choices in each question
    var numChoi = 3;

    // Insert number of questions displayed in answer area
    var answers = new Array(6);

    // Insert answers to questions
    answers[0] = "Alaskan Malamute";
    answers[1] = "Doberman Pinscher";
    answers[2] = "Rottweiler";
    answers[3] = "Pug";
    answers[4] = "Scottish Deerhound";
    answers[5] = "Brussels Griffon";

    //Loops through each question and each choice and compares selected option to correct answer
    function getScore(form){

    var score = 0;
    var currElt;
    var currSelection;
    var p=0;
    while(p<1){
    for (i=0; i<numQues; i++) {
    currElt = i*numChoi;
    for (j=0; j<numChoi; j++) {
    currSelection = form.elements[currElt + j];
    if (currSelection.checked) {
    if (currSelection.value == answers[i]) {
    score++;
    break;
    }
    }
    }
    }
    p++;
    }

    //calculates &#37; score
    score = Math.round(score/numQues*100);

    //assigns % score to value of text box
    form.percentage.value = score + "%";

    //assigns correct answers array to value of textarea box
    var correctAnswers = "";
    for (i=1; i<=numQues; i++) {
    correctAnswers += i + ". " + answers[i-1] + "\r\n";
    }
    form.solutions.value = correctAnswers;
    }





    </script>

    </HEAD>

    <BODY>

    <h3>Breeds Of Dogs Quiz</h3>
    <!-- Quiz Starts Here -->
    <form name="quiz">
    <DL><DT>1. <b>What kind of Dog is this? </b><br>
    <img src="dog images/alaskan_malamute.jpg" width="190" height="174" alt="Alt Text Wont Help You HA!!!!" />
    <DD><input type="radio" name="q1" value="Snowy Mountain Mutt">Snowy Mountain Mutt
    <DD><input type="radio" name="q1" value="Alaskan Malamute">Alaskan Malamute
    <DD><input type="radio" name="q1" value="Alaskan Mauler">Alaskan Mauler
    </DL>
    <DL><DT>2. <b>What kind of Dog is this?</b><br>
    <img src="dog images/doberman_pinscher.jpg" width="190" height="183" alt="Alt Text Wont Help You HA!!!!" />
    <DD><input type="radio" name="q2" value="Doberman Pinscher">Doberman Pinscher
    <DD><input type="radio" name="q2" value="Doberman Pinser">Dobberman Pinser
    <DD><input type="radio" name="q2" value="Doberman Poochy">Doberman Poochy
    </DL>
    <DL><DT>3. <b>What kind of Dog is this?</b><br>
    <img src="dog images/rottweiler.jpg" width="190" height="160" alt="Alt Text Wont Help You HA!!!!" />
    <DD><input type="radio" name="q3" value="Rotter">Rotter
    <DD><input type="radio" name="q3" value="Rotty">Rotty
    <DD><input type="radio" name="q3" value="Rottweiler">Rottweiler
    </DL>
    <DL><DT>4. <b>What kind of Dog is this?</b><br>
    <img src="dog images/pug.jpg" width="190" height="202" alt="Alt Text Wont Help You HA!!!!" />
    <DD><input type="radio" name="q4" value="Pug">Pug
    <DD><input type="radio" name="q4" value="Pog">Pog
    <DD><input type="radio" name="q4" value="Ugly Mutt">Ugly Mutt
    </DL>
    <DL><DT>5. <b>What kind of Dog is this?</b><br>
    <img src="dog images/scottish_deerhound.jpg" width="190" height="172" alt="Alt Text Wont Help You HA!!!!" />
    <DD><input type="radio" name="q5" value="Scottish Duggy">Scottish Duggy
    <DD><input type="radio" name="q5" value="Hairy Mutt">Hairy Mutt
    <DD><input type="radio" name="q5" value="Scottish Deerhound">Scottish Deerhound
    </DL>
    <DL><DT>6. <b>What kind of Dog is this?</b><br>
    <img src="dog images/brussels_griffon.jpg" width="190" height="176" alt="Alt Text Wont Help You HA!!!!" />
    <DD><input type="radio" name="q6" value="Brussels Griffin">Brussels Griffin
    <DD><input type="radio" name="q6" value="Brussels Griffon">Brussels Griffon
    <DD><input type="radio" name="q6" value="Brussels Griffter">Brussels Griffter
    </DL>
    <!-- Calculates Score -->
    <input type="button" value="Get score" onclick="getScore(this.form)">
    <!-- Clears Answers -->
    <input type="reset" value="Clear answers To Start Again">
    <!-- Users Score Revealed Here -->
    <p> Score = <strong><input class="bgclr" type="text" size="5" name="percentage" disabled></strong><br><br>
    Correct answers:<br>
    <!-- Correct Answers Revealed -->
    <textarea class="bgclr" name="solutions" wrap="virtual" rows="6" cols="30" disabled>
    </textarea>
    </form>

    </body>
    </html>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    You have posted your HTML code, but have your attempted to set/read a cookie?

    Cookies have been dealt with very frequently in this forum.

    See for example:-

    http://www.codingforums.com/showthread.php?t=138751
    Last edited by Philip M; 05-03-2008 at 09:32 AM.


  •  

    Posting Permissions

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