Jump to content

Recommended Posts

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>";
?>

Link to comment
https://forums.phpfreaks.com/topic/188063-php-math-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-992820
Share on other sites


<?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>";
?>

Link to comment
https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-992826
Share on other sites

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 
}

Link to comment
https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-992846
Share on other sites

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>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/188063-php-math-help/#findComment-993452
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.