Jump to content

Using a cookie to count the amount of correct answers from a radio button quiz..


gdanelian

Recommended Posts

Hello everyone, thank for your time.

 

I am creating a php quiz.  The first page has question 1 on it with 4 answers as radio buttons.  On answering the user is directed to the second page (quiz2.php) where their answer is printed and the second question is given (this carries on until the user has answered all 5 questions).  I want to create a cookie that keeps track of their correct answers and I also want to print their score out on the final page.  I am totally new to cookies with php...all help welcome! Thanks :)

Link to comment
Share on other sites

#1 Using cookies is fine, but be aware that people can edit their cookies. If this is just a game for fun, then cookies are fine, but if you want to remove that flaw, use SESSIONS

 

For using cookies, there are only a couple things to know. setcookie() writes a value to the cookie. To read the value back, get it from the global variable $_COOKIE. But, the main thing to remember is that after setcookie(), the value won't be in $_COOKIE until the next time a page loads.

Link to comment
Share on other sites

well...you probably don't want to keep a 'count' in the cookie, because if they go back, and answer again, it will increase the value again. instead, keep a 1 or 0 for each question, then add them up.

 

//At this point of the script, i assume the question # (1,2,3 or 4) is in $num
//and $correct is set to 1 or 0 depending on if they go the answer correct

//Load old answers
$answers = array();
for($n = 1;$n <= 4;$n++){
  if(isset($_COOKIE['q'.$n]) && $_COOKIE['q'.$n])
    $answers[$n] = 1;
}
//Add new answer
$answers[$num] = $correct;
setCookie('q'.$num,$correct);
//Get their score
$score = array_sum($answers);

Link to comment
Share on other sites

THIS IS THE FIRST PAGE OF THE QUIZ

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<html>

<head><title>The Quiz</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">

<!--

body,td,th {

color: #FFF;

}

body {

background-color: #000;

}

 

 

</style></head>

<body>

<a href='' onMouseOver='window.alert("The correct answer is either PHP or CSS, good luck")'>Would you like a 50/50?</a>

 

 

<?php

 

?>

<form action="http://www.00000000.ac.uk/~08007934/php/quiz2.php" method="get" class="a1">

<p align="center">WELCOME TO MY COURSEWORK QUIZ...</p>

<p align="center"><strong>Question One: Which os the following was originally designed for producing dynamic web pages </strong></p>

<div align="center">The Universe

  <input type="radio" name="ANSWER" value="The Universe" > <br>

  XHMTL <input type="radio" name="ANSWER" value="XHMTL" > <br>

  CSS <input type="radio" name="ANSWER" value="CSS" > <br>

  PHP <input type="radio" name="ANSWER" value="PHP" > <br>

  <input type="submit" value="SUBMIT YOUR ANSWER">

</div>

</form>

</body>

</html>

 

IF THEY ANSWER CORRECTLY I WANT TO INCREASE A SCORE COUNTER IN A COOKIE AND PRINT THE RESULTS ON THE LAST PAGE.  I assume that I have toset the cookie at the start of page 2? Here is page 2.  I reall appreciate your help but I am just starting with cookies!

 

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<html>

<head><title>The Quiz</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">

<!--

body {

background-color: #000;

}

body,td,th {

color: #FFF;

}

-->

</style></head>

<body>

<a href='' onMouseOver='window.alert("The correct answer is not Mars, good luck")'>Would you like a 50/50?</a>

<div align="center">

 

echo "Your answer is ", $_REQUEST['ANSWER'];

 

if ($_REQUEST['ANSWER'] == "PHP")

{

echo ("<br>Correct, Well done</br>");

}

else

{

echo ("<br>Incorrect</br>");

}

 

?>

</div>

<form action="http://www.0000000ac.uk/~08007934/php/quiz3.php" method="get">

<p align="center">The next question...</p>

<p align="center"><strong>Question Two. Which is the following is a group of interrelated web development techniques used for creating interactive web applications or rich Internet applications: </strong></p>

<div align="center">Michael Jackson <input type="radio" name="ANSWER" value="Michael Jackson" > <br>

  Flash <input type="radio" name="ANSWER" value="Flash" > <br>

  AJAX <input type="radio" name="ANSWER" value="AJAX" > <br>

  Mars <input type="radio" name="ANSWER" value="Mars" > <br>

  <input type="submit" value="SUBMIT YOUR ANSWER">

</div>

</form>

</form>

</body>

</html>

 

 

I am so so confused.

 

Before I set the cooke surely I need to set a score variable e.g. $score

If the user answers the first question correctly then $score = $score +1

if ($answer=="php")

{

($score = $score +1)

}

 

else

{

$score =$score

}

 

Then I need to pass the new score value into the next page for it to be added to if the user answers correctly?

 

THANK YOU SO MUCH, please keep helping me :)

 

Link to comment
Share on other sites

on quiz2.php try this:

<?php
$num = 1;
$correct = 0;
if($_GET['ANSWER'] == "PHP")
  $correct = 1;

//Load old answers
$answers = array();
for($n = 1;$n <= 4;$n++){
  if(isset($_COOKIE['q'.$n]) && $_COOKIE['q'.$n])
    $answers[$n] = 1;
}
//Add new answer
$answers[$num] = $correct;
setCookie('q'.$num,$correct);
//Get their score
$score = array_sum($answers);

?>
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head><title>The Quiz</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
<!--
body {
   background-color: #000;
}
body,td,th {
   color: #FFF;
}
-->
</style></head>
<body>
<a href='' onMouseOver='window.alert("The correct answer is not Mars, good luck")'>Would you like a 50/50?</a>
<div align="center">
<?php
echo "Your answer is ". $_GET['ANSWER'] . "<br>";
echo "So far, your score is ".$score."<br>";

if ($correct)
{
   echo ("<br>Correct, Well done</br>");
}
else
{
   echo ("<br>Incorrect</br>");
}
?>
</div>
<form action="http://www.0000000ac.uk/~08007934/php/quiz3.php" method="get">
<p align="center">The next question...</p>
<p align="center"><strong>Question Two. Which is the following is a group of interrelated web development techniques used for creating interactive web applications or rich Internet applications: </strong></p>
<div align="center">Michael Jackson <input type="radio" name="ANSWER" value="Michael Jackson" > <br>
  Flash <input type="radio" name="ANSWER" value="Flash" > <br>
  AJAX <input type="radio" name="ANSWER" value="AJAX" > <br>
  Mars <input type="radio" name="ANSWER" value="Mars" > <br>
  <input type="submit" value="SUBMIT YOUR ANSWER">
</div>
</form>
</form>
</body>
</html>

Link to comment
Share on other sites

Hello again, I tested the code you gave me for the quiz

On the 5th and final page I use

echo $score;

 

Unfortunately it print '0' no matter how many correct answers I give. I took the following code out of the second page:

 

//Load old answers

$answers = array();

for($n = 1;$n <= 4;$n++){

  if(isset($_COOKIE['q'.$n]) && $_COOKIE['q'.$n])

    $answers[$n] = 1;

}

 

And the score became '1'

 

On the 5th page I want to print the number of correct answers...is there anymore help going? I can't believe how kind you have been so far!!! Thanks

Link to comment
Share on other sites

Hello again!  I am still having trouble getting the cookie to work.

 

Do I have to change any part of the cookie code for different pages?  i.e. on page two do I have to change something in the cookie?

 

Also what if the user presses the back button and answers again...will this mess the cookie up.

 

Finally can you tell me what the different parts of the cookie code mean?

 

<?php

$num = 1;

$correct = 0;

if($_GET['ANSWER'] == "PHP")

  $correct = 1;

 

//Load old answers

$answers = array();

for($n = 1;$n <= 4;$n++){

  if(isset($_COOKIE['q'.$n]) && $_COOKIE['q'.$n])

    $answers[$n] = 1;

}

//Add new answer

$answers[$num] = $correct;

setCookie('q'.$num,$correct);

//Get their score

$score = array_sum($answers);

 

?>

Also does the cookie need to be on all the pages including page one?

 

Sorry to be such a pain!!! Thank you

Link to comment
Share on other sites

If the pages are in different directories, you will run into one problem. But I fixed that in the code below.

 

If they press the back button, and answers again, it will only change that question. That is why I did the more complicated array instead of just a count.

 

The cookie code only needs to be on pages that need it.

 

<?php
//This is the question number to be updated. So it isn't THIS question number, but
//actually the LAST one. So on quiz2.php it would 1, on quiz3.php it would be 2, etc.
$num = 1;

//This is just initiating the variable...leave it alone
$correct = 0;

//Here is the answer test. Again, this is for the LAST question.
if($_GET['ANSWER'] == "PHP"){
  $correct = 1; //If it was correct, we change this variable to 1
}

//This initiates the array that will hold which answers they got correct
//The key will be the question number and the value will be 1 or 0
$answers = array();

//Load old answers from the cookies. This loops from 1 to 4, checks if the
//cookie q# (# being the number 1 to 4) is set. I removed the second part
//that i had before because it wasn't doing anything useful. If the cookie
//exists, it puts the value (1 or 0) into the q# key of the answers array
for($n = 1;$n <= 4;$n++){
  if(isset($_COOKIE['q'.$n]))
    $answers[$n] = 1;
}

//Add new answer. Now that the old answers are loaded, we need to update
//the answer that was just submitted. $num is the question number and $correct
//1 or 0 (we set these above)
$answers[$num] = $correct;
setCookie('q'.$num,$correct,0,'/');
//For setCookie:
//First argument is q#, which is the name of the cookie.
//Second argument is 1 or 0, which is the value of the cookie
//Third argument is the lifetime of the cookie, 0 means the cookie will last until the browser is closed
//Fourth argument is the path of the cookie, '/' means the cookie being set can be accessed by any page on this host

//Get their score. Now that the $answers array is complete, we can just sum it.
//Since every correct answer is 1, the sum will give the number of correct answers
$score = array_sum($answers);

?>

Link to comment
Share on other sites

Thank you very much for the explanation it makes a lot more sense now.  However it does not work correctly.

 

If you answer the questions incorrectly it is still adding to the score.  It works fine if you answer them all correctly...top score is 5, well done etc.

 

Am i a) use the wrong echo (I am using echo "Your current score = "; // echo $score;)

Or b) is the cookie just adding 1 anyway despite it being set to only add one if correct.

 

Thanks yet again.

 

Ill try and get the beer and girls over to you asap...

Link to comment
Share on other sites

PAGE 2:

Your answer is The Universe

Incorrect

Your current score = 0Answers so far:

Array

(

    [1] => 0

)

 

PAGE 3:

Your answer is Michael Jackson

Incorrect

Your current score = 1Answers so far:

Array

(

    [1] => 1

    [2] => 0

)

 

PAGE 4:

Your answer is Toast

Incorrect

Your current score = 2Answers so far:

Array

(

    [1] => 1

    [2] => 1

    [3] => 0

)

 

PAGE 5:

Your answer is George W Bush

Incorrect

Your current score = 3Answers so far:

Array

(

    [1] => 1

    [2] => 1

    [3] => 1

    [4] => 0

)

 

FINAL PAGE:

 

Your answer is Monitor

Incorrect!!!

Your final score = 4Answers so far:

Array

(

    [1] => 1

    [2] => 1

    [3] => 1

    [4] => 1

    [5] => 0

)

 

!? thank you (again!)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.