mstevens Posted October 31, 2014 Share Posted October 31, 2014 I am creating a quiz, with Multiple choice, true and false, and fill in the blanks questions. The first 2 types are working fine, and I am working on tesing the fill the blank questions... See code: //QUESTION 9 if (empty($_POST["ix"])) { $ixErr = "Please select an answer."; } else { $ix = test_input($_POST["ix"]); // check if question only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$ix)) { $ixErr = "Only letters and white space allowed."; else ($ix == "charming") { $score = $score+10; echo "<p>9.) " . $ix . " is correct.</p>\n"; } else { echo "<p>9.) " . $ix . " is incorrect.</p>\n"; } } } First, I am testing to see if it is empty, if yes, display error, otherwise use the test_input function on the entered information. Then I want to test and verify that the entered data only contains characters and/or blank spaces, if that returns yes, then I want to test the content for the correct answer. But for some reason the code gets stuck upon testing for the correct answer, please note: I did try elseif, but same issue, please help, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/292181-testing-a-quiz-question/ Share on other sites More sharing options...
NotionCommotion Posted October 31, 2014 Share Posted October 31, 2014 Hello mstevens, Have you attempted to execute your code? Did it give you any descriptive error descriptions? If not, turn error reporting on! When you do, it will tell you that you can't have back to back else statements and must close your first if-do code. Quote Link to comment https://forums.phpfreaks.com/topic/292181-testing-a-quiz-question/#findComment-1495336 Share on other sites More sharing options...
cyberRobot Posted October 31, 2014 Share Posted October 31, 2014 Perhaps this will work: <?php //QUESTION 9 if (empty($_POST["ix"])) { $ixErr = "Please select an answer."; } else { $ix = test_input($_POST["ix"]); // check if question only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$ix)) { $ixErr = "Only letters and white space allowed."; } elseif ($ix == "charming") { $score = $score+10; echo "<p>9.) " . $ix . " is correct.</p>\n"; } else { echo "<p>9.) " . $ix . " is incorrect.</p>\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/292181-testing-a-quiz-question/#findComment-1495363 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.