...

View Full Version : Setting and reading cookies?



nachoman
05-02-2008, 10:30 PM
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.:thumbsup:

A1ien51
05-03-2008, 01:18 AM
why don't you show us what you have tried.

Eric

nachoman
05-03-2008, 07:29 AM
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>

Philip M
05-03-2008, 08:17 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum