First: This is not a repeat of this question. Same script, but different question.
I had initially thought the error in my previous question would have solved this error, but it didn’t.
I’m making a simple 3-question quiz via PHP. The quiz runs without syntax errors, however the problem is that my variable $correct does not seem to increment throughout the quiz (given a correct answer). I put print statements to see if it changes but nothing gets printed. Somehow its not properly incrementing the value of the variable.
Instead of outputting:
You got 2 out of 3 correct. //where 2 is the value of $correct
It outputs:
You got correct out of correct. //where the value $correct seems to have no value
What am I doing wrong?
<?php
ini_set('session.gc_maxlifetime',900);
//echo ini_get("session.gc_maxlifetime");
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}
if(isset($_SESSION["blah"]))
{
$_SESSION["number"] = 0;
$_SESSION["correct"] = 0;
}
$total_number = 3;
print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;
$number = $_SESSION["number"];
$correct = $_SESSION["correct"];
if ($number == 0){
print <<<FIRST
<p> You will be given $total_number questions in this quiz. <br /><br/>
You will have 15 minutes to complete it. <br /><br/>
You cannot go back to change previous answers.<br /><br/>
Here is your first question: <br /><br />
</p>
<p>1. Abe Lincoln was born in Illinois.</p>
<p>
<label><input type="radio" name="question1" value="true" /> True </label>
<label><input type="radio" name="question1" value="false" /> False </label>
</p>
FIRST;
if (isset($_POST['submit'])) {
$selected_radio = $_POST['question1'];
if ($selected_radio == 'false') {
$correct++;
print $correct;
$_SESSION["correct"] = $correct;
}
}
}
if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
<label><input type="checkbox" name="question2" value="Texas" /> a) Texas
</label><br/>
<label><input type="checkbox" name="question2" value="Pennsylvania" /> b)
Pennsylvania </label><br/>
<label><input type="checkbox" name="question2" value="Virginia" /> c) Virginia
</label><br/>
<label><input type="checkbox" name="question2" value="West Virginia" /> d) West
Virginia </label>
</p>
SECOND;
if (isset($_POST['submit'])) {
if(isset($_POST['question2']) &&
$_POST['question'] == 'Pennsylvania')
{
$correct++;
$_SESSION["correct"] = $correct;
}
}
}
if ($number == 2){
print <<<THIRD
<p>3. The last name of the commander of the Army of North Virginia was __________.</p>
<p>
<input type='text' id='question3' />
THIRD;
if (isset($_POST['submit'])) {
$selected_answer = $_POST['question3'];
if ($selected_answer == "lee") {
$correct++;
$_SESSION["correct"] = $correct;
}
}
}
if ($number >= $total_number)
{
print <<<FINAL_SCORE
Your final score is $correct correct out of $total_number. <br /><br />
Thank you for playing. <br /><br />
FINAL_SCORE;
session_destroy();
}
else
{
$number++;
$_SESSION["number"] = $number;
$script = $_SERVER['PHP_SELF'];
print <<<FORM
<form method = "post" action = $script>
<input type = "submit" value = "Check Answer" />
</form>
FORM;
}
?>
Julio, my current code (2nd Update):
<?php
ini_set('session.gc_maxlifetime',900);
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}
if(isset($_SESSION["correct"])){
$correct = $_SESSION["correct"];
} else {
$number = 0;
$correct = 0;
}
// check if which question was submitted
if (isset($_POST['submit'])) {
// set $number = the question that was submitted
$number = $_POST['question'];
switch ($_POST['question']){
case 1:
if ($_POST['answer']) $correct++; // this answer should be 'true'
break;
case 2:
if ($_POST['answer'] == 2) $correct++; // this answer should be 'PA'
break;
case 3:
if ($_POST['answer'] == "lee") $correct++; //this answer should be 'lee'
}
}
// set the session correct var to our current tally
$_SESSION['correct'] = $correct;
$total_number = 3;
print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;
if ($number == 0){
print <<<FIRST
<p> You will be given $total_number questions in this quiz. <br /><br/>
You will have 15 minutes to complete it. <br /><br/>
You cannot go back to change previous answers.<br /><br/>
Here is your first question: <br /><br />
</p>
<p>1. Abe Lincoln was born in Illinois.</p>
<p>
<label><input type="radio" name="answer" value="true" /> True </label>
<label><input type="radio" name="answer" value="false" /> False </label>
<input type="hidden" name="question" value="1" />
</p>
FIRST;
}
if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
<label><input type="checkbox" name="answer" value="1" /> a) Texas
</label><br/>
<label><input type="checkbox" name="answer" value="2" /> b)
Pennsylvania </label><br/>
<label><input type="checkbox" name="answer" value="3" /> c) Virginia
</label><br/>
<label><input type="checkbox" name="answer" value="4" /> d) West
Virginia </label>
<input type="hidden" name="question" value="2" />
</p>
SECOND;
}
if ($number == 2){
print <<<THIRD
<p>3. The last name of the commander of the Army of North Virginia was __________.</p>
<p>
<input type='text' id='answer' />
<input type="hidden" name="question" value="3" />
</p>
THIRD;
}
if ($number >= $total_number)
{
print <<<FINAL_SCORE
Your final score is $correct correct out of $total_number. <br /><br />
Thank you for playing. <br /><br />
FINAL_SCORE;
session_destroy();
}
else {
$number++;
$_SESSION['number'] = $number;
$script = $_SERVER['PHP_SELF'];
print <<<FORM
<form method = "post" action = $script>
<input type = "submit" value = "Check Answer" />
</form>
FORM;
}
?>
From a quick read of the code it looks like you’re refreshing the page by posting to itself– that means you’re resetting the
$correctvariable to 0 each time. If you want to increment$correct, increment the value in the SESSION, then read out the session$correctvalue into the page’s$correctvar so it isn’t reset on each form submit.EDIT TO ADD SOME SAMPLE CODE: