mardi 8 août 2023

control is matching values of next answer on clicking submit button |php Quiz |random ques quiz| random array quiz & answer matching

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap demo</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
</head>

<body>
  <div class="container">

    <div class="card mt-5">
      <div>
        <h1 class="text-center alert bg-warning">let's Play Quiz</h1>
      </div>
      <div class="card-body">
        <!-- php code for quiz ques ans here -->
        <h5 class="card-title text-center">
         **<?php

          // quesionsssss
          $ques = array(
            "q1" => "Ques:What is currency of India?",
            "q2" => "Ques:Which is the most populated state in India?",
            "q3" => "Ques:Which city is financial capital of India?",
            "q4" => "Ques:Which state has the largest land area ?",
            "q5" => "Ques:Which state is most literate?"
          );
          // answersss
          $answers = array(
            "q1" => "rupee",
            "q2" => "Uttar Pradesh",
            "q3" => "Mumbai",
            "q4" => "rajasthan",
            "q5" => "Kerala"
          );
          $q = array_rand($ques);
          $questions = $ques[$q];
          echo $questions;
          //answer_key
          $ans_key = $answers[$q];
          ?>
**        </h5>
        <form action="" method="post">
          <input type="text" name="ans" required placeholder="Enter your answer here" class="mx-auto my-2 d-block">
          <br>
          <input type="submit" value="Submit Answer" name="subbtn" class="btn btn-warning btn-sm d-block mx-auto">
        </form>
      </div>
      <div class="card-footer bg-warning">
        <small class="text-dark">
        
**  <?php
if (isset($_REQUEST["subbtn"])) {
#             $user_input = $_REQUEST["ans"];
#             $f = strcasecmp($ans_key, $user_input);
if ($f == 0) {
echo "Congratulations";
else {
echo "Better luck Next Time";
#             }
#           }


#           ?>**
        </small>

      </div>
    </div>


</body>


</html>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.min.js" integrity="sha384-Rx+T1VzGupg4BHQYs2gCW9It+akI2MM/mndMCy36UVfodzcJcF0GGLxZIzObiEfa" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous">
</script>

i am expecting to get congratulations message on correct answer and error message on wrong answer.

the ques change everytime page refreshes and same functionality every time.

everytime if enter answer the user input is matched with next answers please help to fix this.

here is how it works

The HTML structure is set up with Bootstrap classes for styling and responsiveness. PHP arrays are used to store quiz questions ($ques) and their corresponding answers ($answers). The code selects a random question from the array using array_rand() and displays it along with an input field for the user's answer. When the user submits their answer, the code compares the user's input with the correct answer ($ans_key) using strcasecmp(), which performs a case-insensitive string comparison. Based on the comparison result, a message is displayed to the user, either "Congratulations" for a correct answer or "Better luck Next Time" for an incorrect answer.




Aucun commentaire:

Enregistrer un commentaire