Dan-O Posted October 2, 2009 Share Posted October 2, 2009 This is my first PHP script, and it’s working, except not as I planned. When I submit an answer the rand variables change making my answer incorrect. How do I correct this and still have a new equation to answer? Here is my code: <?php $x = rand (2, 12); $y = rand (10, 20); $num = $x + $y; print ("$x + $y = "); # print ("$num"); $message = ""; if ( ! isset ($answer) ) $message = "Welcome to my math machine!"; elseif ($answer > $num) $message = "Your answer is too high. Try again."; elseif ($answer < $num) $message = "You answered is too low. You can do this!."; else // correct $message = "Well Done! - Correct Answer -"; ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Randon Number Equation</title> </head> <body> <h4> <?php print $message ?> </h4> <form method="post"> Type your answer here: <input type="text" name="answer" /> <input name="submit" type="submit" value="SUBMIT"> </form> </body> </html> Thanks, Dan Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted October 2, 2009 Share Posted October 2, 2009 well first of all, you aren't even getting the post variable. But you should send the answer through a hidden field. like do something similar to this <html> <body> <?php if (isset($_POST['answer'])){ $answer = $_POST['answer'];//$_POST['answer'] is how you access post variables. its $_POST['input name'] $oldNum = $_POST['oldnum']; echo $answer . " == " . $oldNum."<br />"; } $x = rand (2, 12); $y = rand (10, 20); $num = $x + $y; print ("$x + $y = "); # print ("$num"); $message = ""; if ( ! isset ($answer) ) $message = "Welcome to my math machine!"; elseif ($answer > $oldNum) $message = "Your answer is too high. Try again."; elseif ($answer < $oldNum) $message = "You answered is too low. You can do this!."; else // correct $message = "Well Done! - Correct Answer -"; ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Randon Number Equation</title> </head> <body> <h4> <?php print $message ?> </h4> <form method="post"> Type your answer here: <input type="text" name="answer" /> <input type="hidden" name="oldnum" value="<?php echo $num; ?>" /> <input name="submit" type="submit" value="SUBMIT"> </form> </body> </html> try that. Tested and works for me Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 2, 2009 Share Posted October 2, 2009 If you are concerned about security or someone cheating in a game, you need to remember the answer on the server and pass it between page requests using a session variable. Quote Link to comment Share on other sites More sharing options...
Dan-O Posted October 2, 2009 Author Share Posted October 2, 2009 PFMaBiSmAd : No concerns about cheating. This is almost week two on learning PHP. I thought it would be an easy practice to create an equation script. Apparently it is, but not for me, yet. Mikesta707: Thanks for the repair/finish to my code. I'll have to read it a few more times to fully understand how it works. Dan Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.