2

I am trying to create a webpage that prompts the user to guess a number from 1 to 5 which is randomly generated and stored in the form.

Then, when the form is submitted, the guess is checked against the random number and if the guess is incorrect, the user is prompted to guess again but the random value stays the same with each guess.

My issue is that once the user submits the form, the random number is not showing up when the user is prompted to try again.

else if (isset($_POST["firstname"])) {
    echo $_POST['firstname'] . $_POST['lastname'];
    echo "Hi " . $_POST['firstname'] . " " . $_POST['lastname'] . "!";
    ?>
    <html>
        <form name="number" action="random.php" method="post">
            <p>Enter a guess: <input type="text" name="guess" /></p>
            <input type="hidden" name="numtobeguessed" value="<?php echo $_POST["numtobeguessed"]; ?>" ></p>
            <input type="submit" value="Guess!" />
        </form>
    </html>
    <?php
    $_POST["numtobeguessed"] = rand(1, 5);
    $guessed = $_POST["numtobeguessed"];
    echo "Number to be guessed " . $guessed;

} else if (isset($_POST["guess"])) {
    if ($_POST["guess"] != $_POST["guessed"]) {
        echo $_POST["guess"] . " is not correct";
        ?>
        <form name="number1" action="random.php" method="post">
            <p>Enter a guess: <input type="text" name="guess" /></p>
            <input type="hidden" name="numtobeguessed" value="<?php echo $_POST["guessed"]; ?>" ></p>
            <input type="submit" value="Guess!" />
            <?php
        }
    }
    ?>
8
  • On a side note, I'd recommend using $_POST instead of $_REQUEST. I don't know why you use both. Also, you do not need the == true in the if statements. Commented Dec 3, 2015 at 2:37
  • Are this code are for random.php? Commented Dec 3, 2015 at 2:38
  • Why do you start your code with an else if? Is there code before this that may be interfering? Commented Dec 3, 2015 at 2:39
  • @Jonathan There is code before this that does work. I didn't include it to save space. The code I've posted is where I'm having the issue. Commented Dec 3, 2015 at 2:54
  • Should the logic always check the $_POST["guess"] if it has a value? i.e when $_POST['numtobeguessed'] has a value then the guess can have a value entered by the user? Commented Dec 3, 2015 at 3:23

1 Answer 1

1

You are sending the value in a field with the name numtobeguessed but you try to read it with the name guessed.

This:

if ($_POST["guess"] != $_POST["guessed"]) {

should be:

if ($_POST["guess"] != $_POST["numtobeguessed"]) {

and this:

<input type="hidden" name="numtobeguessed" value="<?php echo $_POST["guessed"]; ?>" ></p>

should be:

<input type="hidden" name="numtobeguessed" value="<?php echo $_POST["numtobeguessed"]; ?>" ></p>
Sign up to request clarification or add additional context in comments.

Comments

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.