Jump to content

Help with simple PHP quiz


Spray62

Recommended Posts

Hi,

 

Just to some up quickly I'm trying to create a simple two page quiz with an answer page and I'm stuck. This is just the rough code as you can see its only one question to try and get it to work before I add more questions. The aim is for the user to answer the questions on the first page then on the second page and the third page displays whether the answers were right or wrong. 

 

 

The code on the first page is 

 

 

<form name="input" action="quizpage2.php" method="post">

Username: <input type="text" name="user">

 

 <p> In which film did Simon Pegg try and impress Thandie Newton by donning tight shorts and trainers? </p>

 

<input type="radio" name='ans1' value="Hot Fuzz"/> Hot Fuzz<br />

<input type="radio" name='ans1' value="Good Luck Chuck"/>  Good Luck Chuck<br />

<input type="radio" name='ans1' value="Run Fatboy Run"/> Run Fatboy Run<br />

<input type="submit" value="Page 2">

 </form>

 

 

The code on the second page is

 

 

<?php

session_start();

$_SESSION['ans1'] = $_POST['ans1'];

 

?>

 

<form name="input" action="answers.php" method="post">

<p> What is Harry Potter? </p>

<input type="radio" name='ans11' value="Wizard"  />  Wizard<br />

<input type="radio" name='ans11' value="Goblin" /> Goblin <br />

<input type="radio" name='ans11' value="Troll"  /> Troll<br />

<input type="submit" value="Submit">

 

  </form>

 

 

And then the answers page

 

 

<?php

session_start();

$_SESSION['ans1] = $_POST['ans1'];

 

?>

 

 

<?php

$ans1=$_SESSION['ans1'];

 

if ($ans1 == "Run Fatboy Run") {

               

                echo "Correct";

               

}

                else {

                               

                                echo "Incorrect the answer is Run Fatboy Run";

                }

?>

 

All it shows on the answer page is "Incorrect the answer is Run Fatboy Run" even when I choose that as the answer.

 

I'm not sure why it isn't working. Whether its the "sessions" I'm doing incorrect or using ' instead of " but I've tried everything and spent around 10 hours doing so. Much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/279717-help-with-simple-php-quiz/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.