CODE
$form_block = "
<p>Quiz</p>
<form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
<p><strong>What's The Capital City Of England?</strong><br>
<input type=\"text\" name=\"q1\" value=\"$q1\" size=30></p>
<p><strong>How Many Letters Are There In The Alphabet?</strong><br>
<input type=\"text\" name=\"q2\" value=\"$q2\" size=30></p>
<p><strong>WHat's 3+10?</strong><br>
<input type=\"text\" name=\"q3\" value=\"$q3\" size=30></p>
<input type=\"hidden\" name=\"execute\" value=\"1\">
<p><input type=\"submit\" name=\"submit\" value=\"Submit\"></p>
</form>";
<p>Quiz</p>
<form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
<p><strong>What's The Capital City Of England?</strong><br>
<input type=\"text\" name=\"q1\" value=\"$q1\" size=30></p>
<p><strong>How Many Letters Are There In The Alphabet?</strong><br>
<input type=\"text\" name=\"q2\" value=\"$q2\" size=30></p>
<p><strong>WHat's 3+10?</strong><br>
<input type=\"text\" name=\"q3\" value=\"$q3\" size=30></p>
<input type=\"hidden\" name=\"execute\" value=\"1\">
<p><input type=\"submit\" name=\"submit\" value=\"Submit\"></p>
</form>";
That takes the form of a form...
You can, of course edit the questions.
I will now break that code down and explain the different parts.
CODE
<form method="POST\" action=\"$_SERVER[PHP_SELF]\">
The reason that any "s (Double quotes) are shown after a \ (Backwards slash) is becouse the PHP Parser will spew out errors if you forget to do this.
"POST" Means that it will carry all the data from this form via POST, rather than GET.
"$_SERVER[PHP_SELF]" Means that it will load this page again to process the data from the form.
CODE
<input type="hidden\" name=\"execute\" value=\"1\">
This will tell the PHP document weather to display the form or process the results.
Then, you're gonna need to display the form
CODE
if ($execute != 1) {
echo "$form_block";
}
echo "$form_block";
}
CODE
if ($execute != 1){
Will find out if "$execute" is not equal to 1. If not, it will display the form like so:
CODE
echo "$form_block";
Submit:
CODE
else if ($op == "ds") {
That'll do the opposite, it "$execute" IS equal to 1, it will process the answers.
Make all the answers in lower case:
CODE
strtolower($q1);
strtolower($q2);
strtolower($q3);
strtolower($q2);
strtolower($q3);
And the answers, you're gonna need answers
CODE
if ($q1 == "london") {
echo "Question 1 corect!<br/>";
$score = "1";
} else {
echo "Try question one again!<br/>";
}
if ($q2 == "26") {
echo "Question 2 correct!<br/>";
$score = $score + 1;
} else {
echo "Try question 2 again!<br/>";
}
if ($q3 == "13") {
echo "Question 3 correct!<br/>";
$score = $score + 1;
} else {
echo "Try question 3 again!<br/>";
}
}
echo "Question 1 corect!<br/>";
$score = "1";
} else {
echo "Try question one again!<br/>";
}
if ($q2 == "26") {
echo "Question 2 correct!<br/>";
$score = $score + 1;
} else {
echo "Try question 2 again!<br/>";
}
if ($q3 == "13") {
echo "Question 3 correct!<br/>";
$score = $score + 1;
} else {
echo "Try question 3 again!<br/>";
}
}
You should be able to understand that, from what i've told you in the rest of the tut.
So the whole code is this:
CODE
<?php
//Make the form
$form_block = "
<p>Quiz</p>
<form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
<p><strong>What's The Capital City Of England?</strong><br>
<input type=\"text\" name=\"q1\" value=\"$q1\" size=30></p>
<p><strong>How Many Letters Are There In The Alphabet?</strong><br>
<input type=\"text\" name=\"q2\" value=\"$q2\" size=30></p>
<p><strong>WHat's 3+10?</strong><br>
<input type=\"text\" name=\"q3\" value=\"$q3\" size=30></p>
<input type=\"hidden\" name=\"execute\" value=\"1\">
<p><input type=\"submit\" name=\"submit\" value=\"Submit\"></p>
</form>";
//Display the form.
if ($execute != 1) {
echo "$form_block";
//OR
}else if ($op == "ds") {
//Process and display the results.
//Make all the user input Lower Case.
strtolower($q1);
strtolower($q2);
strtolower($q3);
//Display results.
if ($q1 == "london") {
echo "Question 1 corect!<br/>";
$score = "1";
} else {
echo "Try question one again!<br/>";
}
if ($q2 == "26") {
echo "Question 2 correct!<br/>";
$score = $score + 1;
} else {
echo "Try question 2 again!<br/>";
}
if ($q3 == "13") {
echo "Question 3 correct!<br/>";
$score = $score + 1;
} else {
echo "Try question 3 again!<br/>";
}
}
?>
//Make the form
$form_block = "
<p>Quiz</p>
<form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
<p><strong>What's The Capital City Of England?</strong><br>
<input type=\"text\" name=\"q1\" value=\"$q1\" size=30></p>
<p><strong>How Many Letters Are There In The Alphabet?</strong><br>
<input type=\"text\" name=\"q2\" value=\"$q2\" size=30></p>
<p><strong>WHat's 3+10?</strong><br>
<input type=\"text\" name=\"q3\" value=\"$q3\" size=30></p>
<input type=\"hidden\" name=\"execute\" value=\"1\">
<p><input type=\"submit\" name=\"submit\" value=\"Submit\"></p>
</form>";
//Display the form.
if ($execute != 1) {
echo "$form_block";
//OR
}else if ($op == "ds") {
//Process and display the results.
//Make all the user input Lower Case.
strtolower($q1);
strtolower($q2);
strtolower($q3);
//Display results.
if ($q1 == "london") {
echo "Question 1 corect!<br/>";
$score = "1";
} else {
echo "Try question one again!<br/>";
}
if ($q2 == "26") {
echo "Question 2 correct!<br/>";
$score = $score + 1;
} else {
echo "Try question 2 again!<br/>";
}
if ($q3 == "13") {
echo "Question 3 correct!<br/>";
$score = $score + 1;
} else {
echo "Try question 3 again!<br/>";
}
}
?>
You can of course, change the questions and answers, add questions and stuff.
When i get time, i'll write a tutorial to make a PHP Quiz with MySQL Questions and Answers.
discuss this topic to forum
