Irate Posted April 30, 2013 Share Posted April 30, 2013 (edited) Hey there, so, basically, I have been reading a few tutorials about making a quiz with PHP, so I decided to write my own quiz, but I am stuck at the answer validation action because I do not know how to properly do so (I am a bit new to PHP, too), so I decided to ask about this. I haven't got a mysql db yet, that noted, and I want the whole quiz to be displayed within an iframe html tag on another site of mine which does not support PHP hosting itself. Code: <?php // quiz.php header("Content-Type: text/html; charset=utf-8"); ?> <!-- exclude basic html skeleton --> <form action="validate.php" method="post"> Question 1: Question Text <input type="radio" value="0">Answer 1 (false, as value="0" indicates)<br> <!-- repeat two more times --> <input type="radio" value="1">Answer 4 (this is my correct answer)<br> <!-- repeat questions until #35 --> <!-- end quiz.php, close html tags --> <?php // validate.php $res = 0; foreach($_POST as $score){ // get all answers from post array $res = $res + $score; } switch($res){ // do switch statement to avoid endless elseif statements because we have many answers case 35: echo "To submit your score, press the button below.<br><form action='http://forum.forumotion.com/posting.forum?' method='post'> <textarea style='display: none;'>Scored ".$res." out of 35 possible points.</textarea> <input type='submit'></form>"; exit; break; case 34: // repeat until reached at 0 echo "..."; exit; break; } ?> The forum URL specified within validate.php is fictional, by the way, but my forum host uses /posting.forum? for its post action. Thing is, I don't want to specify the value of the answers with HTML because about everyone could inspect the HTML code of the iframe and get every answer correct... I want to prevent that, of course. Anyone help? Edited April 30, 2013 by Irate Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/ Share on other sites More sharing options...
Solution Jessica Posted April 30, 2013 Solution Share Posted April 30, 2013 Well, time to learn about databases. Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427265 Share on other sites More sharing options...
budimir Posted April 30, 2013 Share Posted April 30, 2013 Maybe, this could help you... http://php.net/manual/en/function.base64-encode.php Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427266 Share on other sites More sharing options...
Jessica Posted April 30, 2013 Share Posted April 30, 2013 Maybe, this could help you... http://php.net/manual/en/function.base64-encode.php Uhm, how exactly? There's no need to encode the data, simply don't show it to the user. The OP can accomplish this by using arrays in his PHP code, but really he needs to learn to use a database. Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427267 Share on other sites More sharing options...
Irate Posted April 30, 2013 Author Share Posted April 30, 2013 I assume the database would still work within an iframe tag, right? If so, I'll reply to this topic once I have the database doe. Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427270 Share on other sites More sharing options...
Jessica Posted April 30, 2013 Share Posted April 30, 2013 The database is where you store your data. PHP is how you access the data (one of many ways). The fact that your php script is within an iframe in HTML is completely irrelevant. Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427271 Share on other sites More sharing options...
Irate Posted April 30, 2013 Author Share Posted April 30, 2013 Good to know... Thank you for your help and sorry if I'm still too noobish @_@ Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427274 Share on other sites More sharing options...
Jessica Posted April 30, 2013 Share Posted April 30, 2013 As long as you're ready to learn Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427279 Share on other sites More sharing options...
Barand Posted April 30, 2013 Share Posted April 30, 2013 An ini-style text file makes a reasonable alternative to a database for small, simple application of this nature and doesn't require lengthy processing. Here's a simple example quiz1.txt [1] qtext="This is question 1?" A="Q1 Answer 1" B="Q1 Answer 2" C="Q1 Answer 3" D="Q1 Answer 4" correct=C [2] qtext="This is question 2?" A="Q2 Answer 1" B="Q2 Answer 2" C="Q2 Answer 3" D="Q2 Answer 4" correct=A [3] qtext="This is question 3?" A="Q3 Answer 1" B="Q3 Answer 2" C="Q3 Answer 3" D="Q3 Answer 4" correct=B quizform.php <html> <head> <meta name="generator" content="PhpED Version 8.1 (Build 8115)"> <title>My Quiz Form</title> <meta name="author" content="Barand"> </head> <body> <h1>Quiz</h1> <form action='quizresult.php' method='post'> <?php // load the quiz $quiz = parse_ini_file('quiz1.txt', true); // display the quiz foreach ($quiz as $qno => $qdata) { foreach ($qdata as $k => $v) { switch ($k) { case 'correct': continue; case 'qtext': echo "<h3>$v</h3>"; // dummy answer in case no answer given echo "<input type='hidden' name='answer[$qno]' value=''>"; break; default: echo "<input type='radio' name='answer[$qno]' value='$k'> $v<br>"; break; } } } echo "<br><input type='submit' name='btnSubmit' value='Submit'>"; ?> </form> </body> </html> quizresult.php <?php if (!isset($_POST['answer'])) { header("location: quizform.php"); exit; } $quiz = parse_ini_file('quiz1.txt', true); // check answers $qtot = count($quiz); $totCorrect = 0; $results = "<tr><th>Question</th><th>Your answer</th><th>Correct answer</th></tr>"; foreach ($_POST['answer'] as $qno => $ans) { $results .= "<tr><td>{$quiz[$qno]['qtext']}</td><td>{$quiz[$qno][$ans]}</td>"; if ($ans == $quiz[$qno]['correct']) { ++$totCorrect; $results .= "<td>YES</td></tr>"; } else { $results .= "<td>{$quiz[$qno][$quiz[$qno]['correct']]}</td></tr>"; } } ?> <html> <head> <meta name="generator" content="PhpED Version 8.1 (Build 8115)"> <title>My Quiz Results</title> <meta name="author" content="Barand"> </head> <body> <h1>Quiz results</h1> <table border='1'> <?php echo $results ?> </table> <?php printf ("You scored %d out of %d (%0.2f%%)", $totCorrect, $qtot, $totCorrect*100/$qtot); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427299 Share on other sites More sharing options...
Jessica Posted April 30, 2013 Share Posted April 30, 2013 Does depend on how much you want to store. Personally when I think of an application like a quiz, I immediately think of all the functionality you could include and assume you'll want to You could definitely use just a flat file. I'd use something like YAML or XML then. Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427304 Share on other sites More sharing options...
Irate Posted April 30, 2013 Author Share Posted April 30, 2013 Seeing as I want to include 35 Questions with each 2 to 4 radio buttons and 4 essay questions, I think SQL would my weapon of choice once I get around to properly administering my own database, then I think I will be ready to move onto an external server host and will post a new topic about this. Thank you for your input. Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427306 Share on other sites More sharing options...
ignace Posted April 30, 2013 Share Posted April 30, 2013 (edited) You could definitely use just a flat file. YAML, XML, INI. Did everybody forget there are other DB vendors then MySQL? Like SQLite?! A lot easier to switch later on to for example MySQL too. Also if for some reason the application would become more popular supports indexing and other things we have come to expect of a database. Edited April 30, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427339 Share on other sites More sharing options...
Jessica Posted April 30, 2013 Share Posted April 30, 2013 YAML, XML, INI. Did everybody forget there are other DB vendors then MySQL? Like SQLite?! A lot easier to switch later on to for example MySQL too. Also if for some reason the application would become more popular supports indexing and other things we have come to expect of a database. .... Yours was the first post on this thread to mention MySQL Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427370 Share on other sites More sharing options...
ignace Posted May 1, 2013 Share Posted May 1, 2013 (edited) I mentioned MySQL to infer this was more work to setup which is why I suppose everyone opted for XML, YAML, or INI. And I simply wanted to remind everyone there is also SQLite (IMO a far better choice then XML, YAML, or INI). Edited May 1, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427451 Share on other sites More sharing options...
Irate Posted May 1, 2013 Author Share Posted May 1, 2013 I'll give SQLite a try if you say it's easier to set up. I have set-up books about PHP and SQL (I have IIS set up correctly already) but I can't find the rograms because the books are German and my PC language is English >.< Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427454 Share on other sites More sharing options...
ignace Posted May 1, 2013 Share Posted May 1, 2013 (edited) Since you speak german (well a dialect of sorts) and also understand english (since your pc is set to it) I don't think it will be too hard to translate between those two languages. Otherwise if you happen to have Win7 Ultimate edition you can just select a different system language (or using the Anytime Upgrade you can upgrade to ultimate). Edited May 1, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1427466 Share on other sites More sharing options...
ranjeet4rock Posted May 27, 2013 Share Posted May 27, 2013 An ini-style text file makes a reasonable alternative to a database for small, simple application of this nature and doesn't require lengthy processing. Here's a simple example quiz1.txt [1] qtext="This is question 1?" A="Q1 Answer 1" B="Q1 Answer 2" C="Q1 Answer 3" D="Q1 Answer 4" correct=C [2] qtext="This is question 2?" A="Q2 Answer 1" B="Q2 Answer 2" C="Q2 Answer 3" D="Q2 Answer 4" correct=A [3] qtext="This is question 3?" A="Q3 Answer 1" B="Q3 Answer 2" C="Q3 Answer 3" D="Q3 Answer 4" correct=B quizform.php <html> <head> <meta name="generator" content="PhpED Version 8.1 (Build 8115)"> <title>My Quiz Form</title> <meta name="author" content="Barand"> </head> <body> <h1>Quiz</h1> <form action='quizresult.php' method='post'> <?php // load the quiz $quiz = parse_ini_file('quiz1.txt', true); // display the quiz foreach ($quiz as $qno => $qdata) { foreach ($qdata as $k => $v) { switch ($k) { case 'correct': continue; case 'qtext': echo "<h3>$v</h3>"; // dummy answer in case no answer given echo "<input type='hidden' name='answer[$qno]' value=''>"; break; default: echo "<input type='radio' name='answer[$qno]' value='$k'> $v<br>"; break; } } } echo "<br><input type='submit' name='btnSubmit' value='Submit'>"; ?> </form> </body> </html> quizresult.php <?php if (!isset($_POST['answer'])) { header("location: quizform.php"); exit; } $quiz = parse_ini_file('quiz1.txt', true); // check answers $qtot = count($quiz); $totCorrect = 0; $results = "<tr><th>Question</th><th>Your answer</th><th>Correct answer</th></tr>"; foreach ($_POST['answer'] as $qno => $ans) { $results .= "<tr><td>{$quiz[$qno]['qtext']}</td><td>{$quiz[$qno][$ans]}</td>"; if ($ans == $quiz[$qno]['correct']) { ++$totCorrect; $results .= "<td>YES</td></tr>"; } else { $results .= "<td>{$quiz[$qno][$quiz[$qno]['correct']]}</td></tr>"; } } ?> <html> <head> <meta name="generator" content="PhpED Version 8.1 (Build 8115)"> <title>My Quiz Results</title> <meta name="author" content="Barand"> </head> <body> <h1>Quiz results</h1> <table border='1'> <?php echo $results ?> </table> <?php printf ("You scored %d out of %d (%0.2f%%)", $totCorrect, $qtot, $totCorrect*100/$qtot); ?> </body> </html> Hi Barand, I am so much impressed with your quiz solution - I am also trying to execute a PHP quiz test on my website. How can I hold two parameters with each answer and fetch them to calculate result and display it. E.g., Just see it- Q 1. Which is your favorite color? A. Red (P=p1, Q=3) B. White (P=p3, Q=4) C. Green (P=p2, Q= -1) D. Blue (P=p4, Q= -2) Is it possible to hold two parameters with each answer and retrieve the chosen answer. i.e., my user chosen "B" answer of the above question then - How can I fetch associated parameters with white (B - user chosen answer) - It's associated parameters are "(P=p3, Q=4)" which are not visible to the user. Quote Link to comment https://forums.phpfreaks.com/topic/277444-php-quiz-validation/#findComment-1432581 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.