jeudi 22 mars 2018

PHP: How to check the values of the checkbox andd compare with the array?

I am making a quiz application where I have a multiple choice question in PHP. The user gets a question and multiple choices to select the answers. The options what they get will refresh (shuffle/ rand func) everytime. So when the user selects the checkbox and the options gets verified with the correct answer and score them. E.g. if there are 3 right choices and user selects all 3 correct they get 5 points and partial correct they get 3 points and for wrong selections they get 0. Please find my code below. Thanks for your time,

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse"></nav>
<div class="container-fluid text-center">
  <div class="row content">
    <div class="col-sm-2 sidenav">
      <p>&nbsp;</p>
    </div>
    <div class="col-sm-8 text-left">
      <h1>Question 1</h1>
      <p>Multiple choice question</p>
      <div class="row"> </div>
      <hr>
      <p>Which of the following cities are in India?</p>
      <?php
$a=array("London","Mumbai","Berlin","Tokyo","Delhi", "Patna" , "Lahore");
$random_keys=array_rand($a,7);
shuffle($a);?>
      <div class="form-check">
        <form class="form-inline" action="#" method="post">
          <div class="checkbox">
            <label class="form-check-label">
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[0]]?>">
              <?php echo $a[$random_keys[0]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[1]]?>">
              <?php echo $a[$random_keys[1]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[2]]?>">
              <?php echo $a[$random_keys[2]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[3]]?>">
              <?php echo $a[$random_keys[3]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[4]]?>">
              <?php echo $a[$random_keys[4]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[5]]?>">
              <?php echo $a[$random_keys[5]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[6]]?>">
              <?php echo $a[$random_keys[6]]."<br>";?> </label>
          </div>
          <div class="form-group">
            <button type="submit" class="btn btn-default" name="submit">Submit</button>
          </div>
        </form>
      <?php
      $totalscore = 0;
            if(isset($_POST["submit"])){

            if (!empty($_POST["check_list"])){
            echo "<p>  You Selected: </p>";

            foreach ($_POST["check_list"] as $answers)


                echo "<p>".$answers ."</p>";

            }

            else{

            echo "Please make atleast one selection.";  
            }
            }


?>


      </div>
    </div>
    <br>
    <br>
  </div>
</div>
</div>
<footer class="container-fluid text-center">
  <p>SM (c) 2018</p>
</footer>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire