Php Quiz Code

Here is the Php code I made up to create a quiz for my new babysitting website:

<form action=”quiz-results.php” method=”post”>
1. Question 1?<br />
<input type=”radio” name=”question1″ value=”Answer1a” /> Answer1a <br />
<input type=”radio” name=”question1″ value=”Answer1b” /> Answer1b <br />
<input type=”radio” name=”question1″ value=”Answer1c” /> Answer1c <br />
<input type=”radio” name=”question1″ value=”Answer1d” /> Answer1d <br />
<input type=”hidden” name=”q1″ value=”Answer1a” />
<br />

<!– Put more questions here –>
<input type=”hidden” name=”nooqs” value=”Put the number of questions here” />
<center>
<input type=”submit”>
</center>
</form>

—–php code—-

<?php

//Get the number of questions.//
$nooqs = $_POST['nooqs'];

//Get the answer to the question.//
$counter = 1;
$question = “question”;
$q = “q”;
$rightanswers = 0;
$wronganswers = 0;

print “Answers:”;
print “<br />”;

//Begin Loop//
while($counter <= $nooqs){

$correctanswer = $_POST[$q.$counter];
$studentanswer = $_POST[$question.$counter];

//Check the answer//
if ($correctanswer == $studentanswer)
 {
  print “$counter – $correctanswer <br />”;
  $counter = $counter + 1;
  $rightanswers = $rightanswers + 1;
 }
else
 {
  print “$counter – $correctanswer – you answered $studentanswer<br />”;
  $counter = $counter + 1;
  $wronganswers = $wronganswers + 1;
 }

} //End Loop//

//Give Results//

print “<br /><br />”;
print “<h4>You have $rightanswers right answers out of $nooqs questions.</h4>”;

?>
<br />

If you missed any of the questions on this quiz, you should read this website through carefully.<br /><br />

<a href=”http://www.mcgelligot.com”>Quiz Code by mcgelligot</a>

<br /><br />
<hr />

2 Comments

Leave a Reply

You must be logged in to post a comment.

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>