I want to make a quiz test in PHP so that it will instantly show in every page if the answer chosen by the user is correct or not. So my quiz test will be shown as a question per page, please explain and show me how to modify the following code so that I will know what to do in the other pages:
<main>
<div class="big-div">
<p>1. Which is the biggest planet in our Solar System?</p>
<form method="POST">
<label class="container"> A) Uranus
<input type="radio" name="biggestplanet" value="uranus">
<span class="checkmark"></span>
</label>
<label class="container"> B) Saturn
<input type="radio" name="biggestplanet" value="saturn">
<span class="checkmark"></span>
</label>
<label class="container"> C) Jupiter
<input type="radio" name="biggestplanet" value="jupiter">
<span class="checkmark"></span>
</label>
<label class="container"> D) Neptune
<input type="radio" name="biggestplanet" value="neptune">
<span class="checkmark"></span>
</label>
<input type="submit" value="Send" class="submit">
</form>
<?php
$answer1 = $_POST["getAttribute('biggestplanet')"];
if ($answer1 == "C) Jupiter") {
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
} else {
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
}
?>
So, briefly, this is my PHP code that I can't figure out how to write it correctly:
<?php
$answer1 = $_POST["getAttribute('biggestplanet')"];
if ($answer1 == "C) Jupiter") {
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
} else {
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
}
?>
I'm not for the moment interested in how to send the answers to a database, I just want to know how to show to the user if his answers are correct or not.
The <label>
tag is typically bound to an element using the 'for' attribute, which would match the id of the element to which it should be bound. Example:
<label for="uranus">Uranus</label>
<input type="radio" name="biggestplanet" id="uranus" value="Uranus">
<label for="saturn">Saturn</label>
<input type="radio" name="biggestplanet" id="saturn" value="Saturn">
<input type="submit" value="Submit">
And then to grab the value from the input, you'll need to use the 'name' for the input tag:
<form method="POST">
<label for="uranus">Uranus</label>
<input type="radio" name="biggestplanet" id="uranus" value="Uranus">
<label for="saturn">Saturn</label>
<input type="radio" name="biggestplanet" id="saturn" value="Saturn">
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['biggestplanet'])){
if($_POST['biggestplanet'] === 'Saturn'){
$answer1 = $_POST['biggestplanet'];
}elseif($_POST['biggestplanet'] === 'Uranus'){
$answer1 = $_POST['biggestplanet'];
}
echo $answer1;
}
?>
This is just a quick way to write this, I honestly would use a switch statement when making this into a quiz, but I corrected your errors.
If you wanted them to show the correct or incorrect answer without having to click the submit button than you would need to use Ajax to you would not need to refresh the page.
<main>
<div class="big-div">
<p>1. Which is the biggest planet in our Solar System?</p>
<form action="sotest.php" method="POST">
<label class="container"> A) Uranus
<input type="radio" name="biggestplanet" id="uranus" value="uranus">
<span class="checkmark"></span>
</label>
<label class="container"> B) Saturn
<input type="radio" name="biggestplanet" value="saturn">
<span class="checkmark"></span>
</label>
<label class="container"> C) Jupiter
<input type="radio" name="biggestplanet" value="jupiter">
<span class="checkmark"></span>
</label>
<label class="container"> D) Neptune
<input type="radio" name="biggestplanet" value="neptune">
<span class="checkmark"></span>
</label>
<input type="submit" value="Send" class="submit">
</form>
<?php
if(isset($_POST['submit'])){
$answer1 = $_POST['biggestplanet'];
if ($answer1 == "jupiter") {
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
} else {
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
}
}
?>
//////////IN A SWITCH////////
<?php
if(isset($_POST['submit'])){ //stops page from submiting on page refresh
$answer1 = $_POST['biggestplanet'];
switch ($answer1) {
case "uranus":
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
break;
case "saturn":
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
break;
case "jupiter":
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
break;
case "Neptune":
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
break;
}
}
?>
I simplyfied it a bit. (easier to understand)
HTML:
<form method="POST">
<label class="container"> A) Uranus
<input type="radio" name="biggestplanet" value="uranus">
<span class="checkmark"></span>
</label>
<label class="container"> B) Saturn
<input type="radio" name="biggestplanet" value="saturn">
<span class="checkmark"></span>
</label>
<label class="container"> C) Jupiter
<input type="radio" name="biggestplanet" value="jupiter">
<span class="checkmark"></span>
</label>
</form
PHP:
if (isset($_POST['biggestplanet'])) {
$submittedAnswer = $_POST['biggestplanet']; // VALUE of the checked input - identified by NAME of your input
$correctAnswer = 'jupiter';
if ($submittedAnswer === $correctAnswer) {
echo 'Correct!';
} else {
echo 'Sorry, the correct answer is: ' . $correctAnswer;
}
}
If some day you dont know what exactly is submitted. Try a var_dump($_POST);
to show submitted data.