mamm02 Posted January 22, 2018 Share Posted January 22, 2018 (edited) 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 . Edited January 22, 2018 by mamm02 Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/ Share on other sites More sharing options...
mamm02 Posted January 22, 2018 Author Share Posted January 22, 2018 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?" Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555635 Share on other sites More sharing options...
requinix Posted January 22, 2018 Share Posted January 22, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555637 Share on other sites More sharing options...
phpmillion Posted January 22, 2018 Share Posted January 22, 2018 In short, you may want to use PHP header() function. If you have knowledge to make a basic PHP form, this one should be pretty easy to implement. Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555639 Share on other sites More sharing options...
ginerjm Posted January 22, 2018 Share Posted January 22, 2018 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555646 Share on other sites More sharing options...
Stratadox Posted January 22, 2018 Share Posted January 22, 2018 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!"; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555671 Share on other sites More sharing options...
ginerjm Posted January 22, 2018 Share Posted January 22, 2018 I'm sure the OP knows at least some of the basic premises of php's form handling techinques. He wouldn't embark on this new project without doing his homework! I hope... Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555678 Share on other sites More sharing options...
mamm02 Posted January 23, 2018 Author Share Posted January 23, 2018 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555703 Share on other sites More sharing options...
phpmillion Posted January 23, 2018 Share Posted January 23, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555707 Share on other sites More sharing options...
ginerjm Posted January 23, 2018 Share Posted January 23, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555713 Share on other sites More sharing options...
BigB Posted January 24, 2018 Share Posted January 24, 2018 (edited) 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>"; } Edited January 24, 2018 by BigB Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555719 Share on other sites More sharing options...
phpmillion Posted January 24, 2018 Share Posted January 24, 2018 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/306292-how-we-use-php-form-to-make-a-quiz-website/#findComment-1555723 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.