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

  • SEARCH
  • Home
  • 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 6777261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:07:36+00:00 2026-05-26T16:07:36+00:00

First: This is not a repeat of this question . Same script, but different

  • 0

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


?>
  • 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-26T16:07:36+00:00Added an answer on May 26, 2026 at 4:07 pm

    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 $correct variable to 0 each time. If you want to increment $correct, increment the value in the SESSION, then read out the session $correct value into the page’s $correct var so it isn’t reset on each form submit.

    EDIT TO ADD SOME SAMPLE CODE:

    <?php
    
    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']){ //<-----replace this line
            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;
            //etc.
        }
    }   
    
    // 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;
    etc. . .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question might not seem programming related at first, but let me explain. I'm
first of all: this is not the same as this . The ModelBackend has
First look at this url: https://stackoverflow.com/questions/tagged/xoxoxo/ This directory does not exists but somehow stackoverflow
I'm not asking this question because of the merits of garbage collection first of
First of I'm very sorry but this questions is not so so specific. All
this is my first question here :) I know that I should not check
This question might seem a repeat of previous ones. I have read through a
First off, this is NOT a duplicate of: Turn a C string with NULL
First of all I'm not sure this is even possible, however I need to
Just could not get this one and googling did not help much either.. First

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.