PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
if(!isset($_GET['answer'])){
$randomNumber = rand(0,10);
echo "Enter the value you see: <br><form id='validation' name='validation' method='get' action='test2.php'>
<input type='text' name='answer' />
<input type='submit' />
</form>";
echo "<br>Random Number = " . $randomNumber . "<br>";
} else {
if($_GET['answer'] == $randomNumber){
echo "User entered the correct number.";
//echo "<br>Random Number = " . $randomNumber . "<br>";
} else {
echo "User entered the incorrect number";
//echo "<br>Random Number = " . $randomNumber . "<br>";
}
}
?>
When the page is first loaded it generates a form and a random number. Then when the user enters something in the form and submits it, it refreshes the page, and even if the user entered the correct random number first displayed, it returns "User entered the incorrect number" because $randomNumber is null. I know I could obviousally pass the randomNumber through a hidden form field, but that would kind of defeat the whole porpose of my script. I am also aware I could just store the value in a database/text file but I dont want to do that either. Im wondering if it is possable to accomplish this using functions or arrays or something...