|
|
|
|
![]() ![]() |
Jun 26 2006, 10:51 AM
Post
#1
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 39 Joined: 19-June 06 Member No.: 25,361 |
Ok let`s start! Once I wrote it for school:
At first we need questions (php) 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>"; 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"; } 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); 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/>"; } } 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 $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/>"; } } ?> Ofcourse you can change the questions! |
|
|
|
Jul 4 2006, 05:36 AM
Post
#2
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 24 Joined: 4-July 06 Member No.: 26,038 |
That looks really nice, well done! I've been on so many websites that have quizzes and can't figure out how they do it until reading this. Thanks!
|
|
|
|
Jul 4 2006, 08:44 AM
Post
#3
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 39 Joined: 19-June 06 Member No.: 25,361 |
Pleased to help!
|
|
|
|
Jul 4 2006, 10:24 AM
Post
#4
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 58 Joined: 3-July 06 Member No.: 26,004 |
Thanks man, im gonna put this quiz on my new page. It sounds very good.
|
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 13th October 2008 - 10:42 PM |