Making a simple three question PHP quiz. Each question is displayed on a page of its own, when the submit button is pressed, the variable $number should increment by one and its value determines the current page that is displayed on the user’s browser. It appears that somehow this variable is not incrementing correctly — when the submit button is pressed, the page does not change. I would really like to get this program running. I would imagine this is a relatively simple script for most of you. If someone could assist in getting this to run, it would be greatly appreciated.
<?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'] == 'false'){
$correct++; // this answer should be 'false'
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'
break;
}
}
}
// 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;
}
?>
UPDATE:
<?php
ini_set('session.gc_maxlifetime',900);
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}
if(isset($_SESSION["correct"]) and isset($_SESSION["number"])){
$correct = $_SESSION["correct"];
$number = $_SESSION["number"];
} else {
$number = 0;
$correct = 0;
}
// check if which question was submitted
if (isset($_POST['submit'])) {
// set $number = the question that was submitted
$number = $_SESSION['number'];
switch ($_POST['question']){
case 1:
if ($_POST['answer1'] == 'false'){
$correct++; // this answer should be 'false'
break;
}
case 2:
if ($_POST['answer2'] == "PA"){
$correct++;// this answer should be 'PA'
break;
}
case 3:
if ($_POST['answer3'] == "lee") {
$correct++;//this answer should be 'lee'
break;
}
}
}
// 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;
$script = $_SERVER['PHP_SELF'];
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>
<form method = "post" action = "$script">
<label><input type="radio" name="answer1" value="true" /> True </label>
<label><input type="radio" name="answer1" value="false" /> False </label>
<input type="hidden" name="question" value="1" />
<input type = "submit" name = "submit" value = "Check Answer" />
</form>
</p>
FIRST;
}
if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
<form method = "post" action = "$script">
<label><input type="radio" name="answer2" value="Texas" /> a) Texas
</label><br/>
<label><input type="radio" name="answer2" value="PA" /> b)
Pennsylvania </label><br/>
<label><input type="radio" name="answer2" value="Vir" /> c) Virginia
</label><br/>
<label><input type="radio" name="answer2" value="West" /> d) West
Virginia </label>
<input type="hidden" name="question" value="2" />
<input type = "submit" name = "submit" value = "Check Answer" />
</form>
</p>
SECOND;
}
if ($number == 2){
print <<<THIRD
<p>3. The last name of the commander of the Army of North Virginia was __________.</p>
<p>
<form method = "post" action = "$script">
<input type='text' id='answer3' />
<input type="hidden" name="question" value="3" />
<input type = "submit" name = "submit" value = "Check Answer" />
</form>
</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;
}
?>
First, I just have to ask you why you’re using HEREDOC. I’ve never seen someone do it that way.
Anyways, a few things I noticed:
isset($_POST['submit'])will always return false because there is no field with the name ‘submit’.action = $scriptstatement should still include quotations around$scriptfor HTML syntax reasons, otherwise you’ll get something likeaction=/path/to/file.