user3394856 user3394856 - 6 months ago 19
PHP Question

multiplication code not working properly

i have this code where it gives you two random numbers the first time and you have to enter the result of multiplying the two numbers.

if you enter a wrong answer it tells you to guess again but keeps the same random numbers until you answer correctly

if you enter the correct answer it well tell you correct and then starts over with another pair of numbers

the code i provided changes the value of the two rand numbers even if i entered the wrong number. i would like to keep the values the same until the correct answer is entered.

here is my code its very unpolished but its all i could do.

<?php
$num1=rand(1, 9);
$num2=rand(1, 9);
$num3=$num1*$num2;
$num_to_guess = $num3;
echo $num1."x".$num2."= <br>";


if ($_POST['guess'] == $num_to_guess)
{ // matches!
$message = "Well done!";
}
elseif ($_POST['guess'] > $num_to_guess)
{
$message = $_POST['guess']." is too big! Try a smaller number.";
}
elseif ($_POST['guess'] < $num_to_guess)
{
$message = $_POST['guess']." is too small! Try a larger number.";
}
else
{ // some other condition
$message = "I am terribly confused.";
}
?>
<!DOCTYPE html>
<html>
<body>
<h2><?php echo $message; ?></h2>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="answer" value="<?php echo $answer;?>">
<input type="hidden" name="expression" value="<?php echo $expression;?>">
What is the value of the following multiplication expression: <br><br>
<?php echo $expression; ?> <input type="text" name="guess"><br>
<input type="submit" value="Check">
</form>
</body>
</html>

Answer

In order to keep the same numbers, you have to store them on the page and then check them when the form is submitted using php. You must also set the random number if the form was never submitted. In your case, you were always changing num1 and num2. I tried to leave as much of your original code intact, but it still needs some work to simplify it.

  1. First I added 2 more hidden field in the html called num1 and num2
  2. Second, I set $num1 and $num2 to the value that was submitted from the form.
  3. After following the rest of the logic, I make sure that $num1 and $num2 are reset if the answer is correct of it the form was never submitted.

You can see the comments in the code below.

Additionally, if you were going to use this in a production environment, you would want to validate the values being passed in from the form so that malicious users don't take advantage of your code. :)

 <?php
// Setting $num1 and $num2 to what was posted previously and performing the math on it.
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num_to_guess = $num1*$num2;

// Check for the correct answer
if ($_POST && $_POST['guess'] == $num_to_guess)
{
    // matches!
    $message = "Well done!";
    $num1=rand(1, 9);
    $num2=rand(1, 9);
} 
// Give the user a hint that the number is too big
elseif ($_POST['guess'] > $num_to_guess)
{
    $message = $_POST['guess']." is too big! Try a smaller number.";
} 
// Give the user a hint that the number is too small
elseif ($_POST['guess'] < $num_to_guess) 
{
    $message = $_POST['guess']." is too small! Try a larger number.";
} 
// If the form wasn't submitted i.e. no POST or something else went wrong
else  
{
    // Only display this message if the form was submitted, but there were no expected values
    if ($_POST)
    {
        // some other condition and only if something was posted
        $message = "I am terribly confused.";
    }
    // set num1 and num2 if there wasn't anything posted
    $num1=rand(1, 9);
    $num2=rand(1, 9);
}

// Show the problem
echo $num1."x".$num2."= <br>";

?>
<!DOCTYPE html>
<html>
<body>
<h2><?php echo $message; ?></h2>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="num1" value="<?= $num1 ?>" />
<input type="hidden" name="num2" value="<?= $num2 ?>" />
<input type="hidden" name="answer" value="<?php echo $num3;?>">
<input type="hidden" name="expression" value="<?php echo $expression;?>">
What is the value of the following multiplication expression: <br><br>
<input type="text" name="guess"><br>
<input type="submit" value="Check">
</form>
</body>
</html>
Comments