richo89 Posted January 11, 2010 Share Posted January 11, 2010 Hi, I'm trying to use PHP math to create maths questions and test the user. I'm currently generating random numbers and asking the user to add the two numbers, I then want to check if the answer is correct. However, when I click submit the random numbers change as page refreshes and therefore my variable changes so it keeps saying incorrect answer. Code is below: <?php // Question 1. $number1 = rand(1,10); $number2 = rand(5,15); $answer1 = $number1 + $number2; echo "<p>"; echo "<b>Q1.</b> $number1 + $number2 = <form name='answer1form' method='post' action='index.php'> <input type='text' size='1' name='answer1'/>"; echo "</p>"; echo "<input type='submit' name='Submit' value='Submit'>"; if($_POST['Submit']){ echo "<p>"; echo $_POST['answer1']; echo "</p>"; echo "<p>"; echo $answer1; echo "</p>"; if ($_POST['answer1'] == $answer1) { echo "<p>Correct answer</p>"; } else { echo "<p>Incorrect answer</p>"; } } echo "</form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/188063-php-math-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 11, 2010 Share Posted January 11, 2010 The only thing that ties any HTTP requests together is what the browser supplies when it makes the HTTP request. You would either need to pass the answer in a hidden field in the form (not secure), in a COOKIE (not secure) or using session variables on the server. Quote Link to comment https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-992820 Share on other sites More sharing options...
Lamez Posted January 11, 2010 Share Posted January 11, 2010 <?php session_start(); // Question 1. $_SESSION['num1'] = $number1 = rand(1,10); $_SESSION['num2'] = $number2 = rand(5,15); $_SESSION['ans'] = $answer1 = $number1 + $number2; echo "<p>"; echo "<b>Q1.</b> $number1 + $number2 = <form name='answer1form' method='post' action='index.php'> <input type='text' size='1' name='answer1'/>"; echo "</p>"; echo "<input type='submit' name='Submit' value='Submit'>"; if($_POST['Submit']){ echo "<p>"; echo $_POST['answer1']; echo "</p>"; echo "<p>"; echo $_SESSION['ans']; echo "</p>"; if ($_POST['answer1'] == $_SESSION['ans']) { echo "<p>Correct answer</p>"; } else { echo "<p>Incorrect answer</p>"; } } echo "</form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-992826 Share on other sites More sharing options...
richo89 Posted January 11, 2010 Author Share Posted January 11, 2010 Thanks for the help just tried the above code but it still seems to refresh the variables each time so I keep getting incorrect answer. Quote Link to comment https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-992842 Share on other sites More sharing options...
PFMaBiSmAd Posted January 11, 2010 Share Posted January 11, 2010 That's because the code is unconditionally assigning the values to the session variables, so they will be replaced on each page request. You need to separate your form code from your form processing code - if($_POST['Submit']){ // code to process the form goes here } else { // code to produce the form goes here, including setting any session variables you expect to have a specific value when the form is processed } Quote Link to comment https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-992846 Share on other sites More sharing options...
richo89 Posted January 11, 2010 Author Share Posted January 11, 2010 Ok so when I click submit the IF needs to process the form which is checking if the answer equals user input. the ELSE produces the form, so by this do you mean the form name, method etc. Quote Link to comment https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-992871 Share on other sites More sharing options...
richo89 Posted January 12, 2010 Author Share Posted January 12, 2010 Can anyone try and help me further i'd be very grateful.. Quote Link to comment https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-993421 Share on other sites More sharing options...
PFMaBiSmAd Posted January 12, 2010 Share Posted January 12, 2010 One way to accomplish what you are doing - <?php session_start(); if(isset($_POST['Submit'])){ // code to process the form goes here echo "<p>"; echo $_POST['answer1']; echo "</p>"; echo "<p>"; echo $_SESSION['answer1']; echo "</p>"; if ($_POST['answer1'] == $_SESSION['answer1']) { echo "<p>Correct answer</p>"; } else { echo "<p>Incorrect answer</p>"; } } else { // code to produce the form goes here, including setting any session variables you expect to have a specific value when the form is processed $_SESSION['number1'] = $number1 = rand(1,10); $_SESSION['number2'] = $number2 = rand(5,15); $_SESSION['answer1'] = $number1 + $number2; echo "<p>"; echo "<b>Q1.</b> $number1 + $number2 = <form name='answer1form' method='post' action=''> <input type='text' size='1' name='answer1'/>"; echo "</p>"; echo "<input type='submit' name='Submit' value='Submit'>"; echo "</form>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-993452 Share on other sites More sharing options...
richo89 Posted January 12, 2010 Author Share Posted January 12, 2010 Thank you kindly it works perfectly, i'll try figure it out now and further it. Thanks again, PFMaBiSmAd. Quote Link to comment https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-993515 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.