Jump to content
xisto Community
maddog39

Php Quiz Script Make quizzes for your site.

Recommended Posts

Hello all,

 

A little bit back I decided to make a quiz scriptjust out of no where lol. However it doesnt do anything special but I am going to make an email mod for it so that it will email results to your email address. So here is the basis of it.

 

INSTRUCTIONS:

Open a new page in your text editor and paste in the following code.

<?php$qid = "Quiz ID-00";?><html><head><title><? echo "Gamers Pub $qid"; ?></title></head><body><p><h3><? echo "SiteName $qid"; ?></h3></p><form action="results.php" method="post"><p>Username: <input type="text" name="name"></p><p>1.) Question number one is?<br><input type="radio" name="q1" value="Answer1"> Answer1<br><input type="radio" name="q1" value="Answer2"> Answer2</p><p>2.) Question number two is?<br><input type="radio" name="q2" value="Answer1"> Answer1<br><input type="radio" name="q2" value="Answer2"> Answer2</p><p>3.) Question number three is?<br><input type="radio" name="q3" value="Answer1"> Answer1<br><input type="radio" name="q3" value="Answer"> Answer2</p><p>4.) Question number four is?<br><input type="radio" name="q4" value="Answer1"> Answer1<br><input type="radio" name="q4" value="Answer2"> Answer2</p><p>5.) Question number five is?<br><input type="radio" name="q5" value="Answer1"> Answer1<br><input type="radio" name="q5" value="Answer2"> Answer2</p><p><input type="submit" name="submit" value="Submit Quiz"></p><input type="hidden" name="qp" value="quiz00.php"></form></body></html>
and then save it as quiz00.php and edit it obiously, lol to what you want. I shouldnt be hard because its mostly just html. But NOTE that if you go over 5 questions then you need to modifiy the php script that shows the results to display those added questions.

 

Here is the results page which displays the results from the forms. Open up a new page in your text editor and paste in the following code.

<?php$title = "Quiz Results";echo "<title>$title</title>";// Below gets the answers from the page before //if (isset ($_POST['submit'])) {   $name = $_POST['name'];   $q1 = $_POST['q1'];   $q2 = $_POST['q2'];   $q3 = $_POST['q3'];   $q4 = $_POST['q4'];   $q5 = $_POST['q5'];   $qp = $_POST['qp'];}// Below checks to see if you forgot anything //if ($name == "") {   die ("You forgot something, go back and check over your quiz.");}if ($q1 == "") {   die ("You forgot something, go back and check over your quiz.");}if ($q2 == "") {   die ("You forgot something, go back and check over your quiz.");}if ($q3 == "") {   die ("You forgot something, go back and check over your quiz.");}if ($q4 == "") {   die ("You forgot something, go back and check over your quiz.");}if ($q5 == "") {   die ("You forgot something, go back and check over your quiz.");}// Below is where the answers are actually displayed //{	echo<<<EOT<p>Results: $name<br>1.) $q1<br>2.) $q2<br>3.) $q3<br>4.) $q4<br>5.) $q5</p><p><a href="$qp">Go Back To Quiz?</a>EOT;}?>
and then save it as results.php. Also this time I put comments in there which should give you some hints to what is going on in the process of getting and displaying the answers. I will be making a mod soon that will be added on to this and it will email the forms to a selected email address so that they are sent to something to be reviewed by a person... you know what I mean?

 

Also if you want a demo, I have one set up on my website:

Demo: CLICK HERE

 

If you need any help or some more understanding of the script then just ask by reply ing to this post. Enjoy! :)

Share this post


Link to post
Share on other sites

Hi maddog39,Very nice idea/ implementation. Can't wait to see the e-mail forwarding function/ mod. Been looking for something like this for a while. How would you go about making this an exit questionaire(pop up etc.) for your website when visitors leave?RGPHNX

Share this post


Link to post
Share on other sites

I would probabl put some Javascripts to do it in my front page and then insert sessions into the pages so it knows when to do it and to see if the person already saw the quiz, things like that. :)

Share this post


Link to post
Share on other sites

I think thats a little hard. That would be easier with flash or something prolly. I am still learning php and I can do all this stuff lol. Anyway thanks for all the completments. I should have that mod by tomorrow because im kinda busy today. :)

Share this post


Link to post
Share on other sites

Okay guys. Im sorry it took so long ive just been kinda really busy. Anyway here it is with the email mod. All you have to do is open up results.php, erase the hole entire thing and paste in this new code. I had to kinda mod the hole thing so its just easier to do it this way. So heres the code.

<?php$title = "Quiz Results";echo "<title>$title</title>";if (isset ($_POST['submit'])) {   $name = $_POST['name'];   $q1 = "1.)" . $_POST['q1'];   $q2 = "2.)" . $_POST['q2'];   $q3 = "3.)" . $_POST['q3'];   $q4 = "4.)" . $_POST['q4'];   $q5 = "5.)" . $_POST['q5'];   $qp = "6.)" . $_POST['qp'];}if ($name == "") {   die ("You forgot something, go back and check over your quiz.");}if ($q1 == "1.) ") {   die ("You forgot something, go back and check over your quiz.");}if ($q2 == "2.) ") {   die ("You forgot something, go back and check over your quiz.");}if ($q3 == "3.) ") {   die ("You forgot something, go back and check over your quiz.");}if ($q4 == "4.) ") {   die ("You forgot something, go back and check over your quiz.");}if ($q5 == "4.) ") {   die ("You forgot something, go back and check over your quiz.");}{	echo<<<EOT<p>Results: $name<br>$q1<br>$q2<br>$q3<br>$q4<br>$q5</p><p><a href="$qp">Go Back To Quiz?</a>EOT;}$Name = $_POST['name'];$Subject = "Quiz Results";$Email = "You@mail.com";$Message = "Here are the quiz results.";$align = $_POST['align'];$to = "$EmailTo";$subject = "$Subject";$body = "$Message\n\nQuiz By: $Name\n$q1\n$q2\n$q3\n$q4\n$q5";mail($to,$subject,$body);header("Location: sent.htm");?>
and change the things you need to change. You will need to change the '$Email' variable to your email address inside the quotes and you will also need to make another page called sent.htm which would probably look something like this...

<html><head><title>Message Sent</title></head><body><p>Your quiz has been sent to the webmaster. You should recieve a reply shortly. </p><p><a href="http://yoursite.trap17.com/index.php" target="_self">Go Back To Index?</a></p></body></html>
which will tell the person that there quiz was sent. Also change'yoursite' yo your actual website URL so that they may go back to you homepage. :):)

Share this post


Link to post
Share on other sites

I did a little improving on the results.php page:

// Below gets the answers from the page before and checks to see if you forgot anythingwhile (extract($_POST) == '' && isset($_POST['submit'])){	die("You forgot something, go back and check over your quiz.");}// make some valid html with newlinesecho "<html>\n<head>\n<title>\nQuiz Results\n</title>\n</head>\n<body>\n";echo "Results: $name<br />\n";for ($i = 1; $i <= 5; $i++) {	echo "$i.) ${q . $i}<br />\n";}echo '<a href="' . $qp . '">Go Back To Quiz?</a><br />';echo "\n</body>\n</html>";

Share this post


Link to post
Share on other sites

Wow.... meh.... I kinda got really lost when it got to the make some valid html newlines and im not sure what that means. Plus the point of this is to improve the understanding to the newbs and I hate making things overly complicated.

Share this post


Link to post
Share on other sites

I just ment to make a valid html page, because you only included the <title> and some <p>'s. You missed <html>, <head> and <body>. The newlines are coded in php as \n. They will output the code after it on a new line, so you don't get <html><head><title>etc.. but start with a new line for each important html element.

Share this post


Link to post
Share on other sites

That doesnt really matter. It still works and does the same thing. Just because you dont have the <html> tags does not nessaraly mean that its not going to work.

Share this post


Link to post
Share on other sites

That's your opinion on valid code. I know that your quiz script works, but it would have no real markup and wouldn't look very professional. I prefer valid coding, and without those other tags you can't really make a site layout using css code and such.The goal of my code snippet was not to improve html though, it was to show that you could do the same with much less php code.

Share this post


Link to post
Share on other sites

awesome dude! i dont really like taking quizzes but it's kinda cool to have

Share this post


Link to post
Share on other sites

Well you could make a survey or something out of it by changing the titles and stuff. I just decided to call it a quizz scripts.

Also...

That's your opinion on valid code. I know that your quiz script works, but it would have no real markup and wouldn't look very professional. I prefer valid coding, and without those other tags you can't really make a site layout using css code and such.
The goal of my code snippet was not to improve html though, it was to show that you could do the same with much less php code.

The point wasnt to make it professional, its just code to put into your website. So to me it doesnt really matter and I dont think it really matters to anyone else to because alot of peoples sites arent professional and if they want it to look like that then they can do that but the point of the tutorial was not for looks or "valid html pages" because that doesnt really matter either.

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

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