0

I have a PHP page that defines random variables using mt_rand() and then uses html forms to get two user inputs. And depending on whether the input numbers are equal to the randomised numbers outputs "correct".

However I have the problem that when I reload the page by submitting the values from the forms my randomised values change and the answers are always wrong. I have looked and found the solution of AJAX (J-Query function of stopping reloading). I haven't used Javascript before but will learn it if needed. My question is whether there is another way to go about getting the values and determining whether they are the same as my randomised values or will I need to stop the page from reloading upon submission?

My code is below:

<html>
<?php

$x1 = mt_rand(-12,12);
$x2 = mt_rand(-12,12);


echo "$x1<br>$x2<br>";

?>

<form action="es_algebra.php" method="POST">
<input type="number" name="x1"/>
<input type="number" name="x2"/>
<input type="submit" value="Submit"/>
</form>

<?php
if ($_POST['x1'] == $x1)
{
    echo "Correct";
}
else
{
    echo "Wrong";
}

?>
</html>

1 Answer 1

2

Store the random values in session-variables.

<?php
session_start();
$x1 = mt_rand(-12,12);
$x2 = mt_rand(-12,12);
$_SESSION['x1'] = $x1;
$_SESSION['x2'] = $x2;

...

<?php
session_start();
if ($_POST['x1'] == $_SESSION['x1']) {
    [...]

Final code:

<?php session_start(); ?>
<html>
<?php
if (!isset($_POST['submit'])) {
    // Generate random numbers
    $x1 = mt_rand(-12, 12);
    $x2 = mt_rand(-12, 12);

    // Store
    $_SESSION['x1'] = $x1;
    $_SESSION['x2'] = $x2;
}

// Debug output
echo $_SESSION['x1'] . '<br />' . $_SESSION['x2'] . '<br />';
?>
<form action="" method="post">
    <input type="number" name="x1"/>
    <input type="number" name="x2"/>
    <input type="submit" value="Submit" name="submit" />
</form>
<?php
if (isset($_POST['submit'])) {
    if ($_POST['x1'] == $_SESSION['x1']) {
        echo 'x1 correct';
    }
    else {
        echo 'x1 wrong';
    }

    echo '<br />';

    if ($_POST['x2'] == $_SESSION['x2']) {
        echo 'x2 correct';
    }
    else {
        echo 'x2 wrong';
    }
}
?>
</html>
Sign up to request clarification or add additional context in comments.

5 Comments

I have put in the code you suggested. I am getting the notice: "Notice: A session had already been started - ignoring session_start() in C:\wamp\www\alg.php on line 20". However I am still getting the wrong answer. I have found it still does what it did before. It only finds the input value correct for the new reloaded randomised value.
@user3788874 Oh, you have everything in the same file? That won't work. Everything from the second php-tag should be in another file. Or else you'll have to rewrite your code, so the session is not written at every pageload.
I'm sorry but now I am confused. If they are in two separate files which one am I supposed to run and how does it connect to the other file?
I have checked your answer. I am just not understanding how each file links to each other, when I put them in separate files nothing seems to happen.
@user3788874 - With the final code, you can just save the entire thing as one file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.