mercredi 8 février 2023

Print different results for different "checkbox" in PHP

Im doing an assignment and I got stuck.

I am preparing a project divided in 5 different .php files. The first page is an introduction page, where the user can type name and get started. 3 middle pages are for tests and quiz. The 5th and last page is a summary of the results of the previous tests. The second test is an <input type="checkbox" ...> and, according to the answer given, I want to print different results.

  • If the 2 answers are correct echo= "Correct"
  • If only 1 of the answers is correct echo= ""One of the two answers is wrong"
  • If only 1 box was checked echo= "Answer is incomplete"
  • No box was checked echo= "You did not select any option" This is the HTML
<p>Through mixing which of the following primary colors can the secondary color in the figure be obtained?</p>
<form action="quiz_3.php" method="POST">
<label><input type="checkbox" name="result_quiz_2[]" value="yellow">Yellow</label>
<label><input type="checkbox" name="result_quiz_2[]" value="red">Red</label>
<label><input type="checkbox" name="result_quiz_2[]" value="blue">Blue</label>
  
<input type="submit" value="Confirm & Go to Quiz 3"><br>

With this code in the next page I store the answers in $_SESSION

<?php
session_start();
$q_2 = $_POST;
if (array_key_exists('result_quiz_2',$q_2))
{
$_SESSION['result_quiz_2'] = $_POST['result_quiz_2'];
}
else $_SESSION['result_quiz_2'] = 0;
?>

What should i write on the final file to print out the different results?

foreach () 

doesnt work if no boxes are checked and

isset() 

will print "Correct" if just one answer of the two is correct.

really appreciate for whoever can help.




Aucun commentaire:

Enregistrer un commentaire