I’m working on a personal project and part of the idea involves the user answering five questions and, depending on the responses, it will navigate you to one of the 3 different stories. I am currently using a simple quiz from CSS-Tricks as a template and I’ve modified it to accept all answers as correct by using the elseif statement. The issue I have now is that I’m not entirely sure on how to make it point to one of the different files based on the responses.
The script works by asking questions (as you can see on the site, if you desire) which is output here, collected and displayed.
It’s relatively simple in that all the “A” answers point to one story, all the “B” answers point to another and the same for “C”. Obviously, due to the nature of the quiz I’m trying to edit, it wants to “grade” them rather than collect the results and make a decision based on that.
<?php
$answer1 = $_POST['question-1-answers'];
$answer2 = $_POST['question-2-answers'];
$answer3 = $_POST['question-3-answers'];
$answer4 = $_POST['question-4-answers'];
$answer5 = $_POST['question-5-answers'];
$responseA = 0;
$responseB = 0;
$responseC = 0;
if ($answer1 == "A") { $responseA++; }
elseif ($answer1 == "B") {$responseB++; }
elseif ($answer1 == "C") {$responseC++; }
if ($answer2 == "A") { $responseA++; }
elseif ($answer2 == "B") {$responseB++; }
elseif ($answer2 == "C") {$responseC++; }
if ($answer3 == "A") { $responseA++; }
elseif ($answer3 == "B") {$responseB++; }
elseif ($answer3 == "C") {$responseC++; }
if ($answer4 == "A") { $responseA++; }
elseif ($answer4 == "B") {$responseB++; }
elseif ($answer4 == "C") {$responseC++; }
if ($answer5 == "A") { $responseA++; }
elseif ($answer5 == "B") {$responseB++; }
elseif ($answer5 == "C") {$responseC++; }
if ($responseA) { echo '<a href="sin.php">Next Page</a>'; }
elseif ($responseB) { echo '<a href="mys.php">Next Page</a>'; }
elseif ($responseC) { echo '<a href="ils.php">Next Page</a>'; }
?>
So, if anybody could point me in the right direction, I’d appreciate it. It sounded simple enough when I thought it up but it’s far trickier than I planned for a learning project!
EDIT
I’ve updated the code to reflect cbronson’s suggestions and it certainly works a lot better than before! The problem I have now is that it seems responseC is not being counted. The first two variable’s are working perfectly and any combination of those will result in A or B but when I select even all C, it spits out the link to “mys.php” rather than with “ils.php”.
EDIT 2
Got it working again. Silly, silly, silly. Thanks for all your help!
Instead of a single response variable, create three separate variables for each answer (A,B,& C) and add one to each var respective of the test results. Then whichever response variable has more results (was chosen most frequently), will be your overall answer. Tie breaking will be up to you.
For example, if I answer the quiz with: 1)A 2)B 3)C 4)A 5)A; The variables should be: responseA = 3, responseB = 1, responseC = 1. Variable response A is greater than B and C so point me to the story for response A.