Jump to content

Testing a quiz question


mstevens

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/292181-testing-a-quiz-question/
Share on other sites

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.

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";
    }
}
?>

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.