So here’s my problem:
I want to make a small quiz app using PHP, and here’s my code unitl now –
<?php
$questions = [
['What is 10 + 12?','23','19','22','21'],
['What is 19 + 21?','212','2','-15','40','80'],
['What is 3^4?','1987','27','81','33'],
['What is 2^5?','32','64','10','8']
];
$answers = ['21','40','81','32'];
$currentques = 1;
$index = $currentques - 1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quizzes</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container border shadow" name="questiondiv" id="questiondiv">
<form action="" method="POST">
<p name="questiontext">
<?php
echo $questions[$index][0];
?>
</p>
<?php
$options = count($questions[$index]) - 1;
for($i = 0;$i < $options;$i++)
{
$lindex = $i + 1;
echo '<input type="radio" name="option" id="option'.strval($lindex).'">';
echo '<label for="option'.strval($lindex).'">'.$questions[$index][$lindex].'</label><br>';
}
?>
<button type="submit" name="btnnext">Next</button>
</form>
</div>
</body>
</html>
When I click on the ‘Next’ button to submit the form, I want to save the answer (I know how to do that), change the $currentques value and then ‘refresh’ the form/dynamically generate it again, this time to show the next question and its respective options. This will go on until I reach the last question and create an array called $useranswers. I will compare this with my $answers array and generate a score.
I am confused regarding the ‘refresh’/re-dynamically generating part. I would be grateful for any help.