Would be awesome if you could help me out! I am trying to build a simple page to display random math questions. The answers should be typed in by the user and should be validated on the same page, giving him feedback on success and failure.
The problem is that by submitting the form input to the same page (I called it "form6.php") the page reloads and a new set of numbers is generated - that is why the "solution" variable is newly defined and cannot be used to test the accuracy of the user's answer.
This is the code I have so far (works fine for fixed numbers but fails with the random number generation):
<?php
$number1 = rand(1,100);
$number2 = rand(1,100);
$solution = $number1+$number2;
echo "$number1+$number2=?";
?>
<form action="form6.php" method="post">
Your Answer:<br>
<input type="integer" name="answer">
<input type="Submit" value="Submit!">
</form>
<?php
if(isset($_POST['answer'])){
if ($_POST["answer"] == $solution)
{echo "That's right";}
else
{echo "That's wrong!";};
}
?>
Any help is highly appreciated! Since I am not a professional coder, the more specific you can get, the better!