Jump to content
xisto Community
Sign in to follow this  
tempest

Quiz With Php, But Without Mysql

Recommended Posts

Ok let`s start! Once I wrote it for school:

At first we need questions (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>";

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.

<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.

<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

if ($execute != 1) {echo "$form_block";}

if ($execute != 1){

Will find out if "$execute" is not equal to 1. If not, it will display the form like so:

echo "$form_block";

Submit:

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:

strtolower($q1);strtolower($q2);strtolower($q3);

And the answers, you're gonna need answers

	 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:

<?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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.