I am trying to create a quiz whereby user will key in their answer in a textfield. If the answer the user keys in matches with the answer in the array, the score will increase. However my $answer only returns the last value the user entered which does not allow comparison of the user's input with the array, and the score does not increase. Please advice on how to allow the textfield to read the user's input and compare it with the array's answer provided.
<?php
$qtspick_key = array_rand($qtspool, 3);
$pickqts = array();
$i = 0;
foreach ($qtspick_key as $key) {
$pickqts[$i] = $qtspool[$key];
$i++;
}
$qtspool = array(
1 => array(
'qts' => 'When was The Fast and the Furious released? ', 'ans' => '2001'),
2 => array(
'qts' => 'When was The Fast and the Furious: Tokyo Drift released? ', 'ans' => '2006'),
3 => array(
'qts' => 'When was Fast & Furious Presents: Hobbs & Shaw released? ', 'ans' => '2019'),
4 => array(
'qts' => 'When was Insidious released? ', 'ans' => '2010'),
5 => array(
'qts' => 'When was Insidious: Chapter 2 released? ', 'ans' => '2013'),
6 => array(
'qts' => 'When was Insidious: Chapter 3 released? ', 'ans' => '2015'),
7 => array(
'qts' => 'When was Insidious: The Last Key released? ', 'ans' => '2018'),
8 => array(
'qts' => 'When was World War Z released? ', 'ans' => '2013'),
9 => array(
'qts' => 'When was The Conjuring released? ', 'ans' => '2013'),
10 => array(
'qts' => 'When was The Conjuring 2 released? ', 'ans' => '2016')
);
?>
<form action="" method="post">
<?php $score = 0; ?>
<?php foreach($pickqts as $qtsno => $value) { ?>
<?php echo $value['qts'] ?>
<input type="text" name="user_ans">
<?php echo $value['ans'] ?><br><br>
<?php if (isset($_POST["user_ans"])) { ?>
<?php $answer = $_POST["user_ans"]; ?>
<?php if ($value['ans'] == $answer) { ?>
<?php $score++; ?>
<?php } ?>
<?php var_dump($value['ans']); ?>
<?php var_dump($answer); ?>
<?php } ?>
<?php } ?>
<p> Current score: <?php echo $score ?></p>
<input type="submit" value="Submit Quiz"/>
</form>