Soren Jakobsen
Soren Jakobsen

Reputation: 13

Using $_POST variable on different php pages

EDIT: Thanks for all the helpful answers, I got it solved.

I can't really seem to find an answer to this question, and it's probably really simple.

I was just creating a page for fun where you can guess a number from 1-10. Person 1 enters a secret number, that person 2 will guess. However, I have had a lot of problems storing the $_POST from the secret number.

TL;DR I can't store the information of secretNumber into guessnumber_guessed.php file. For example look in guessnumber_guessed. First part of the if statement, if the inputNumber equals the secretNumber, it should say correct. Problem is, the variblae is undefined, how to a 'transfer the info'?

Hope you guys get my point, help is really appreciated

Here's the code:

guessnumber_welcome:

<form method="post" action="guessnumber.php">
    <input type="text" name="secretNumber" placeholder="Type the secret number"> 
    <input type="submit" name="submit" value="Send">
</form>

guessnumber.php:

<form method="post" action="guessnumber_guessed.php">
        <input type="text" name="inputNumber" placeholder="Guess the secret number"> 
        <input type="submit" name="submit" value="Guess!">
    </form>
<?php
        $secretNumber = $_POST["secretNumber"]

    ?>

guessnumber_guessed.php:

<form method="post" action="">
    <input type="text" name="inputNumber" placeholder="Guess the secret number"> 
    <input type="submit" name="submit" value="Guess!">
</form>

<?php
$inputNumber = $_POST["inputNumber"];

if ($inputNumber == $secretNumber) {
    echo "<p id=\"correctAnsw\"> CORRECT! </p>";

}
    else if ($inputNumber == 2) {
    echo "<p id=\"wrongAnsw\">You're very close. Go up a little!</p>";

    }

    else if ($inputNumber==4) {
    echo "<p id=\"wrongAnsw\">You're very close. Go down a little!</p>";


    }

    else if ($inputNumber > 10) {
    echo "<p id=\"wrongAnsw\">The number is you guessed is too high. Stay within the borders!</p>";

    }

    else if ($inputNumber < 1) {
    echo "<p id=\"wrongAnsw\">The number is you guessed is too low. Stay within the borders!</p>";

    }

    else {
    echo "<p id=\"wrongAnsw\">This is not the number. Try a new one!</p>";

    }
?>

Upvotes: 0

Views: 1422

Answers (2)

Lorenzo Zottar
Lorenzo Zottar

Reputation: 436

I faced a similar problem trying to pass beetween two scripts some url strings for a "two-step" uploader. In my opinion there is two solutions, depends on the level of security you want to have:

  • In guessnumber.php put the $_POST['secretNumber'] value in an input type="hidden"

    <input type="hidden" value="<?php echo $_POST['secretNumber']; ?>">
    

    In this way the value will be passed to the second script via POST and will be available in the $_POST array.

    This method, is not safe for sensible datas, because everybody who can access the html source simply through the browser devtool can read or modify it!!

  • The second, and more safe, solution is to use the php session
    In guessnumber.php start the php session and save the value in this way:

    if ( !session_id() ) {
        session_start();
    }
    $_SESSION['secretNumber'] = $_POST['secretNumber'];
    

    then in guessnumber_guessed.php recover the session and get the value from there

    if ( !session_id() ) {
        session_start();
    }
    $secretNumber = $_SESSION['secretNumber'];
    

I strongly recommend the second solution.
Hope it helps :)

Upvotes: 1

Alex
Alex

Reputation: 810

The sessions will not help. They're user oriented. If the first person is using the page to enter a number and the second person comes after that, in the same session, on the same browser, you can use the sessions mechanism.

If you're trying to make a multi-user "game" and the two persons are with separate browsers, it means you must:

  1. Pair the two persons somewhat (maybe a room mechanism)
  2. Use some kind of server storage or cache (you can even use memcached for in-memory storage) to match the two persons and their answers.

Upvotes: 2

Related Questions