Jump to content

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";
    }
}
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.