-1

I'm trying to get the page after clicking submit to tell the user if they got the question correct or not. As of right now, it always says its correct, regardless if what's typed into the text box is the correct answer or not. Any help would be greatly appreciated thanks.

Here is the code in question. The first part is from the html file creating the questions.

 <form action="Graded.php" method="post" id="quiz">
    Question 7: How many signs are in the Chinese zodiac?
    <input type="text" name="question-7-answers" value="" required>

    Question 8: Which is the only reptile that is a sign in the Chinese zodiac?
    <input type="text" name="question-8-answers" value="" required>

    Question 9: Which is the only imaginary animal that is a sign in the Chinese zodiac?
    <input type="text" name="question-9-answers" value="" required>

    Question 10: Which is the only bird that is a sign in the Chinese zodiac?
    <input type="text" name="question-10-answers" value="" required>

    <input type="submit" value="Submit Quiz"/>
    <input type="reset" value="Reset Quiz" class="reset_button"/>
</form>

Here is the php part that checks to see if the answer is correct and posts the result on the page after clicking submit.

$answer7 = $_POST['question-7-answers'];
$answer8 = $_POST['question-8-answers'];
$answer9 = $_POST['question-9-answers'];
$answer10 = $_POST['question-10-answers'];


if ($answer7 == "12" || "twelve") { $totalCorrect++; echo "Answer 7 was Correct <br />\n";}
else echo"Incorrect answer. This is the correct answer: Question 7: How many signs are in the Chinese zodiac?    Answer: 12 <br />\n";

if ($answer8 == "Snake" || "snake") { $totalCorrect++; echo "Answer 8 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 8: Which is the only reptile that is a sign in the Chinese zodiac? Answer: Snake <br />\n";

if ($answer9 == "Dragon" ||  "dragon") { $totalCorrect++; echo "Answer 9 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 9: Which is the only imaginary animal that is a sign in the Chinese zodiac?   Answer: Dragon <br />\n";

if ($answer10 == "Rooster" || "rooster") { $totalCorrect++; echo "Answer 10 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 10: Which is the only bird that is a sign in the Chinese zodiac?    Answer: Rooster <br />\n";

Here is the updated code with the fixed if statements

if ($answer7 == "12" ||  $answer7 == "twelve") { $totalCorrect++; echo "Answer 7 was Correct <br />\n";}
else echo"Incorrect answer. This is the correct answer: Question 7: How many signs are in the Chinese zodiac?    Answer: 12 <br />\n";

if ($answer8 == "Snake" || $answer8 == "snake") { $totalCorrect++; echo "Answer 8 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 8: Which is the only reptile that is a sign in the Chinese zodiac? Answer: Snake <br />\n";

if ($answer9 == "Dragon" || $answer9 == "dragon") { $totalCorrect++; echo "Answer 9 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 9: Which is the only imaginary animal that is a sign in the Chinese zodiac?   Answer: Dragon <br />\n";

if ($answer10 == "Rooster" ||  $answer10 == "rooster") { $totalCorrect++; echo "Answer 10 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 10: Which is the only bird that is a sign in the Chinese zodiac?    Answer: Rooster <br />\n";
bilss
  • 15
  • 4

2 Answers2

2

your if conditions are wrong.

They should be if ( $answer7 == "12" || $answer7 == "twelve")

Also keep in mind that == and === perform different comparisons: How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?

Update: you should clean your input from any trailing whitespace using trim and force lowercase using strtolower:

$answer7 = strtolower(trim($answer7));
Community
  • 1
  • 1
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
0

Your conditions like :

if ($answer7 == "12" || "twelve") 

Should be either

if ($answer7 == "12" || $answer7 == "twelve") 

or

if ( in_array($answer7, array("12","twelve")  )

Your checks is always true due to your 2nd condition "twelve". Not empty strings casted into boolean are interpreted as true.

[Edit with full exemple]

$questions = array(
   7 => array(
    'question' => "How many signs are in the Chinese zodiac ?",
    'reponses' => array("12","douze")
   ),
   8 => array(
    'question' => "Which is the only reptile that is a sign in the Chinese zodiac ?",
    'reponses' => array("snake")
   ),
   9 => array(
    'question' => "Which is the only imaginary animal that is a sign in the Chinese zodiac ?",
    'reponses' => array("dragon")
   )
);

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

    $totalCorrect = 0;

    foreach( $questions as $num => $question ) {
        $input = "question-{$num}-answers";
        if( isset($_POST[$input]) ) {
           if( in_array(strtolower(trim($_POST[$input])), $question['reponses']) ) {
              $totalCorrect++; 
              echo "Answer {$num} was Correct";
           }
           else 
              echo "Incorrect answer. This is the correct answer: Question {$num} : {$question['question']}    Answer : {$question['reponses'][0]}";
           echo "<br />\n"
        }
    }
}

You could even accept any small typing error with functions like similar_text or levenshtein for not integer responses.

p5yk0
  • 1
  • 1