如何检查随机值等于输入值以在帖子上输入php

I'am new to php and I have no idea why my code in php is always echoing FALSE. I do not want to use another hidden input like:

<input type="hidden" name="storeRandVal" value="<?php echo $randomValue; ?>

to store my generated random value, and then checking if the entered value in input is equal with value that was generated and entered to hidden input. Is there any way around to do it in php also without involving cookies?

Here is my code:

<?php
$buttonPost = $_POST['button_post']; 
$enteredValue = htmlspecialchars(trim($_POST['test_input_p']));
$randomValue = rand(1,100);

if(isset($buttonPost))
{
    if($randomValue == $enteredValue)
    {
        echo "TRUE";
    }
    elseif($randomValue != $enteredValue)
    {
        echo "FALSE";
    }
    else
    {
        echo "Er__!";
    }
}
?>

<html>
    <head>
        <meta></meta>
    </head>
    <body>
        <form action="" method="post">
            <fieldset>
                <label for="test_input" id="label_input">Enter value: <?php echo $randomValue; ?></label>
                <input id="test_input" name="test_input_p">
                <input type="submit" id="ibutton_send" name="button_post" value="Send">
            </fieldset>
        </form>
    </body>
</html>

Why not store the random value in a session? Re-set the session only when a form is not submitted (eg; when a user comes to this page from a different page)

As @Fred commented, you have to include the hidden input. By its nature, PHP's rand() function gives you a different answer every time you load the page.

You'll need to compare the $_POST['test_input_p'] and $_POST['storeRandVal'] values in order to confirm that the user entered the correct values.

Here's what you can do:

Store the hidden value in a variable then compare it with that value.

(Unless you will be using this for more than 2 pages, sessions are not needed, not for this since you're using your entire code inside the same page.)

<?php
$buttonPost = $_POST['button_post']; 
$enteredValue = htmlspecialchars(trim($_POST['test_input_p']));

$hidden = $_POST['storeRandVal'];
$randomValue = rand(1,100);

if(isset($buttonPost))
{

    if($enteredValue == $hidden)
    {
        echo "TRUE";
    }
    elseif($randomValue != $hidden)
    {
        echo "FALSE";
    }
    else
    {
        echo "Er__!";
    }
}
?>

<html>
    <head>
        <meta></meta>
    </head>
    <body>
        <form action="" method="post">

<input type="hidden" name="storeRandVal" value="<?php echo $randomValue; ?>">

            <fieldset>
                <label for="test_input" id="label_input">Enter value: <?php echo $randomValue; ?></label>
                <input id="test_input" name="test_input_p">
                <input type="submit" id="ibutton_send" name="button_post" value="Send"></input>
            </fieldset>
        </form>
    </body>
</html>