I’m trying to build a quiz
This is a sample of a try catch that I am using. The main thing I am trying to create is an online quiz and when the “submit” button is clicked I want the results to print to the screen.
The try catch works but it is pointless right now because it does not require user interaction. The results are hard coded.
This is an example of my my html and try catch that I have so far.
My question is how to I get that to occur?
<p>Which Basketball team is known as the Hoosiers
<select>
<option value="">--Select--</option>
<option value="IU">IU</option>
<option value="Kentucky">Kentucky</option>
<option value="Ohio State">Ohio State</option>
<option value="Connecticut">Conneticut</option>
</select>
</p>
</br>
<input type="submit" id="submit" name="submit" value="submit"/>
</form>
<?php
$quiz = 'IU';
try {
//The answer is correct
if ($quiz == 'IU') {
echo 'Well Done!!!.';
}else{
throw new Exception ('Not even close.');
}
//That is incorrect
}catch (exception $wrg) {
echo 'Error: ' .$wrg->getMessage();
}
?>
I think the question you’re asking is how do you read in the results from the quiz. You can do this through the global $_POST variable, you will need to name your select though like so
<select name="answer">and then you can access it through $_POST[‘answer’] to get the selection value. Then you can compare it, you should not be using a throw exception with a try-catch block here, it’s not really designed for using it for program logic. Instead you should just try a simple if else in this case e.g.