Search code examples
phpstrcmp

PHP Quiz not working properly


I'm trying to make a simple php quiz,but instead of choices I have to insert answers and compare them with strcasecmp() so it won't be a problem if the first letter is uppercase or anything like that,but the code doesn't work properly.Sometimes it doesn't return the correct result,even though I insert the correct answer. Here is the code:

<?php
         $number = rand(1,2);
         $result = "";
         $answer = "";
         $question = "";

         $question1 = "What is the capital of China";
         $question2 = "What king of country is China?";

         $answer1 = "beijing";
         $answer2 = "republic";

         if ($number == 1) {
             $answer = $answer1;
             $question = $question1;
         }

         if ($number == 2) {
             $answer = $answer2;
             $question = $question2;
         }

         if(isset($_POST['submit'])){
            if (strcasecmp($answer,$_POST['answer']) == 0) {
                 $result = "Correct!";
            } else {
                 $result = "Wrong!";
            }
         }

    ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="style.css">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>

        <form method="post" action="index.php">
            <div class="middle">
                <p class="question"><?php echo $question; 
                ?></p>
                <p class="result"><?php echo $result;
                 $result = "" ?></p>
                 <div class="box">
                   <input type="text" name="answer" placeholder="Type here" class="text">
                 </div>
            </div>
            <input type="submit" name="submit" value="Continue" class="btn">
        </form>
    </body>
    </html>

Solution

  • By looking at your code, we can see that when you submit your form, you are restarting the script, which actually resets $random to a new value. With 2 questions, you have 50% chances to have the 'correct' answer, but you would see that your script doesn't work at all with more questions added.

    Basically, you should use another way to achieve what you want. You could try to add the id of the question in an hidden <input> and check when your form is submit to determine which one it was.

    if(isset($_POST['submit'])){
       switch ($_POST['question']) { // Add '{'
       case 1:
           if (strcasecmp($answer1,$_POST['answer']) == 0) {
                $result = "Correct!";
           } else {
                $result = "Gresit!";
           }
           break;
       case 2:
            if (strcasecmp($answer2,$_POST['answer']) == 0) {
                $result = "Correct!";
           } else {
                $result = "Gresit!";
           } // Forgot to Add '}'
           break;
        } // Add '}' It give error in PHP 5.3 Parse error: syntax error, unexpected T_CASE, expecting ':' or '{'
    }
    

    For the HTML, you could add this input in your form :

    <input type="text" name="question" value="<?php echo $number ?>" hidden>
    

    This is not the best way to achieve what you want, this is just an example of what would work.