I hope you will thank me for this HTML and PHP quiz script
Get information from a group of radio buttons you should start off by giving them all the same name but different values.
With a quiz, it would seem sensible to make the value of the radio button true for the correct answer and false for all the rest. So, for example, your first question could be coded as such:
CODE
<form action="answers.php" method="post">
<p>Question one typed here</p>
<input type = 'radio' name ='1' value= 'false'>False answer one<br />
<input type = 'radio' name ='1' value= 'false'>False answer two<br />
<input type = 'radio' name ='1' value= 'true'>Correct answer<br />
<input type = 'radio' name ='1' value= 'false'>False answer three<br />
<input type="submit" name="submit" value="Submit Answers">
</form>
Server side (PHP)
You would repeat this for every question, changing the name to the question number for each set.
Then, once you have coded the form you would need to add some php to handle the answers. I would do it in a seperate page but you can do it is the same page if you wish. I created a page called answers.php and when you click the submit button all the values from the selected radio boxes are submitted to that page ready to be processed.
First you should check that the form has been submitted and someone has not found their way to the answers page directly a simple if statement works well for this:
CODE
//This checks to see if the submit button was clicked
if (isset($_POST['submit'])) {
}
Any code you write to handle the scoring should be placed in this statement.
As you know how many questions there are you can use a for statement to loop through each question and retrieve the answer given increasing the score if it is correct.
CODE
<?php
if (isset($_POST['submit'])) {
//first initialise the score variable
$score = 0;
//for statement this will loop through 50 times
for ($i=1; $i<51; $i++){
$answer = $_POST[$i];
// for each iteration this sets the variable $answer to the value of the
//the question number i so on the first iteration i = 1 so we are looking
//at question number one.
//if statement to test the value of $answer
if ($answer == "true"){
//increase score by one
$score++;
}
}
echo "Your score is: $score";
}
?>