tags:

views:

89

answers:

1

I need a little extra help to figure out an issue I'm having. You can see a great deal of detail about it here: http://stackoverflow.com/questions/2134056/checking-correct-answer-and-submitting-form, but here's the basics.

  1. Project is a just-for-fun quiz composed of 10 question pages and 1 result page.
  2. Submitting a Form composed of 3 Radio buttons, 1 is correct.
  3. Security is not an issue.
  4. Cannot use JavaScript, all PHP/HTML/CSS (mobile browsers and limitations etc.)
  5. Form action takes the user to the next page.

How can I select the correct answer (can be hardcoded) and how can I save the number of right answers as a user continues through the pages?

<?php 
    /* Gets current correct answer Count */ 
    $answer_count = $_GET["p"]; 

    /* checks to see if the submitted answer is the same as the correct answer */
    if ($_POST["submitted-answer"] == "correct-answer") {
        $answer_count++;
    }
?>

The form HTML:

<form name="quiz" action="" method="POST">
<label for="o1"><input type="radio" name="grp" id="o1" value="o1"> Label 1</label>
<label for="o2"><input type="radio" name="grp" id="o2" value="o2"> Label 2</label>
<label for="o3"><input type="radio" name="grp" id="o3" value="o3"> Label 3</label>
<input type="submit" value="Next Question" class="btn">
</form>

The values of my radio buttons are not necessary at the moment. Can I use something utilizing value="wrong" on the two wrong answers and value="right" on the correct one?

+1  A: 

To track the number of correct answers you would probably want to use PHP sessions. Just add session_start(); at the beginning of the code, set some session variable to zero at the first question and destroy the session after the quiz is finished.

<?php
session_start();
if ([check if first question and unanswered]) {
  $_SESSION['correct_answers'] = 0;
}
if ([check if question answered and correctly]) {
  $_SESSION['correct_answers']++;
}
if ([check, if that was the final question]) {
  [do some magic with the number of correct answers]
  $_SESSION['correct_answers'] = 0; // no turning back now!
}

Something like that.

Ninjastyle