Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:23:18+00:00 2026-05-26T15:23:18+00:00

Making a simple three question PHP quiz. Each question is displayed on a page

  • 0

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;
}


?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:23:18+00:00Added an answer on May 26, 2026 at 3:23 pm

    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:

    1. All of your input fields are being printed above and outside of your form, not sure if that it intentional, but it appears that it is not. Only things inside the actual form will be submitted with that form. Your form needs to surround all of your input fields that you want sent with it and your submit button.
    2. Your submit button has no name. You can’t retrieve the value of the submit button unless you name it ‘submit’, for example. Thus isset($_POST['submit']) will always return false because there is no field with the name ‘submit’.
    3. Your action = $script statement should still include quotations around $script for HTML syntax reasons, otherwise you’ll get something like action=/path/to/file.
    4. What @MrTrick said should help you as well, although that won’t kill your script.
    5. For your answers, you are incorrectly using the checkbox type. You should be using the radio type so that only one option can be selected.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple quiz database, where a question has an answer and one
Is there a simple way of making the static content of an .aspx page
Simple question: what is the proper way of making a geo-spatial query with mongoose
I'm making a simple quiz application which has multiple questions. It has no back
I'm making a fairly simple text editor, and I have a question about my
I'm making a simple web server using PHP and sockets. Everything is working fine
I have a simple question about MySQL and PHP here. Let's say I have
I am making a simple CMS to use in my own blog and I
This is a rather simple question that is making me a bit curious. Consider
Somewhat related to my earlier question. I'm making a simple html parser to play

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.