Jump to content

How we use PHP form to make a quiz website?


mamm02

Recommended Posts

Frankly I am  Beginner   and  I started to make simple  website  (quiz: questions and answers ) by  using  HTML and CSS only and  I want to use PHP forms  but I need help to find the codes that do this in simple way  : 

 

 

 

1-If the user insert  the the right answer in the form  this will lead him to new PHP file .

 

2-If  the user insert  any  wrong  answer in the form this will lead him to different PHP file .

Link to comment
Share on other sites

Can you write/post the code that does everything except for send the user to the next file?

 

I am so sorry I could not write a full title and I don,t know how this happen . I don,t know how to fix this .I wanted to use this title" How we use PHP form to make a quiz website?"

Editing is only allowed for a short time after posting. I've changed the thread title for you.
Link to comment
Share on other sites

Why do you want o use different forms? Is this a requirement from your teacher? It should be pretty simple to use some php logic to decide where to go in one single script, thus avoiding duplication of code that would be common to two scripts.

Link to comment
Share on other sites

PHP has a simple way of checking the results of a form: its the $_POST array.

 

You can do, for example:

 

Html:

<form>
<p>What is the answer? <input name="question" type="text"></p>
<p><input type="submit" value="Answer"></p>
</form>
PHP:

<?php
 
if ($_POST['question'] === 'The expected answer') {
  echo "That is the expected answer!";
} else {
  echo "That is not the expected answer!";
}
Link to comment
Share on other sites

I want to say thanks for everyone here try to help .

 

Let me explain what I my trying to do again because I  left  few details :

 

 ·    If  the user insert  the right answer in the form  this will lead him to  a new PHP file ( or a  new window) that   tell tell him that his answer is right and contain link to  the next question .

 

 

2- If  the user insert  any  wrong  answer in the form this will lead him to different PHP file( or new window )that tell the user that the that his answer is wrong  and contain link to  go back to last question .

 

All what I can do for now is this : 


<html>

<body>
<form action="check3.php" method="post" >
<p>Who is the writer of Hamlet?</p>


 <input name="question" type="text"></p>
<p><input type="submit" value="Answer"></p>
</form>

</body>
</html>

and this  the PHP file: 


<?php

if ($_POST['question'] === 'Shakespeare') {
  echo "your answer is right!";

    <a href="puzzl.html"> go to the next question! </a>


} else {
  echo "your answer is wrong! ";
    <a href="test.html"> try again </a>
}


but  the results is not good and I don,t know why ? 

 

 

 

 

Link to comment
Share on other sites

No offense, but this is kinda stupid. You can display correct/wrong notifications of multiple questions using a single PHP file. That is to say, creating a separate PHP file for every question is the worst quiz creation strategy you could ever think of. Why do you want to make your work extremely difficult and time consuming?

 

Also, you make a huge flaw in your quiz this way. Your example says that if my answer is right, I get redirected to puzzl.html. This way, I can skip any question and manually type yourdomain/com/somefile.html into my browser and open any question without even answering previous questions. Sure, there's a way to prevent it using sessions and/or cookies, but why not make your quiz properly in first place?

Link to comment
Share on other sites

I understood what you were attempting from your first post. What I didn't understand I was pretty clear about.

 

As for what you just posted, I think you need to stop coding and step back and think. To do one question at a time is fine if that is the way you want. But to handle each question with a different script (?) is not the right way. Plus - you say "if the answer is right" you go to one form and if it is not right you go to another form. Well, how do you know if it is right or not without going to SOME form??

 

I see a script like this - one script to do everything.

 

1 - if no incoming post data, output the first question to your user

2 - if a proper incoming post is received, check the question's answer to see if it is right. I would hide the question number in the form to use to look up the answer in my database.

3 - if the answer is right I would call a function to record the result. Then I would call a function to retrieve the question that follows the one just answered. Display the whole input form again and wait.

4 - if the answer is wrong, I would either send the form back with an error message, or accept it as wrong. Record the result. Look up the next question. Display the whole input form again and wait.

 

All of this would be separate blocks of code in your one script with the html output being done entirely at the end of the script. Good design has a script written with the setup at the top (session vars, get input values, etc.), separate secionts of logic and functions, and then the html output. That means NOT outputting the headers and JS and CSS and <body> tags until the very end when you send everything out. Use php vars for the dynamic parts such as the output messages or values in the appropriate places in your html.

 

This will make for one concise script that will be very readable and easy to follow. Use functions for things such as retrieveing a question, for recording a result and for checking an answer. This will make it very easy to build. Be sure to build in error handling to check for mistakes during your development period. Check each function during the process before moving on so that you build this whole thing block by block.

 

Most important is to think about your project before sitting down at the keyboard.

Link to comment
Share on other sites

Hi @mamm02

 

Your code looks close, try below.

 

NOT TESTED: 

if ($_POST['question'] === 'Shakespeare') {
      
	echo "your answer is right! <br/><br/><a href='puzzl.html'> go to the next question! </a>";

} else {

	echo "your answer is wrong! <br/><br/><a href='test.html'> try again </a>";
   
}
Link to comment
Share on other sites

 

Hi @mamm02

 

Your code looks close, try below.

 

NOT TESTED: 

if ($_POST['question'] === 'Shakespeare') {
      
	echo "your answer is right! <br/><br/><a href='puzzl.html'> go to the next question! </a>";

} else {

	echo "your answer is wrong! <br/><br/><a href='test.html'> try again </a>";
   
}

 

All you did was rewriting OP's code to remove HTML stuff and display everything using pure PHP, which doesn't change anything. Also, please do NOT encourage OP to use the worst possible coding. Because such a code is simply not acceptable.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.