Generate the Equation and Save the Result:
1 2 3 4 5 6 7 8 9 10 |
session_start(); $digit1 = mt_rand(1,20); $digit2 = mt_rand(1,20); if( mt_rand(0,1) === 1 ) { $math = "$digit1 + $digit2"; $_SESSION['answer'] = $digit1 + $digit2; } else { $math = "$digit1 - $digit2"; $_SESSION['answer'] = $digit1 - $digit2; } |
Display the Equation in your Form:
1 |
<?php echo $math; ?> = <input name="answer" type="text" /> |
Validate the Text Entered by the User:
1 2 3 4 5 6 7 8 |
<?php session_start(); if ($_SESSION['answer'] == $_POST['answer'] ) { // value entered is correct } else { // value is incorrect, kindly try again } |
Leave a reply