vendredi 26 février 2016

Random Number Resets

My random number generates on page load, but seems to reset when the user clicks the "Guess" button. I still have a lot of building to go with this, but at the end I want the user to be able to make multiple guesses to guess the random number. I want it to generate when the page first comes up and stay the same until correctly guessed. I'm not sure what I'm doing wrong and just starting this program. If you answer, please also explain, as I'm trying to learn what I'm doing. Thank you!

<!doctype html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>PHP Homework 2</title>
        <link rel="stylesheet" href="styles/styles.css">
    </head>

    <body>
        <section id="main">
            <h1>Play the Guessing Game!</h1>
            <section id="left">
                <h2>Take a Guess!</h2>
                <form action="mine.php" method="post">
                    <div id="guessBox">
                        <label id="guessLB">Your Guess:</label>
                        <input id="guessTB" class="num" type="number" name="guessTB" max="1000" min="1">
                        </input>
                    </div>
                    <input type="submit" id="guessButton" name="guessBTN" value="Guess" />
                </form>
            </section>
            <section id="right">
                <h2>Game Stats</h2>
                <?php 

                    if(isset($_POST['guessTB'])) 
                    {
                        $randomNum = $_POST['guessTB'];
                    } 
                    else 
                    {
                        $randomNum = rand(1,100);
                    }                       

                    echo "<p>Random Number: $randomNum</p>";
                ?>      
            </section>
        </section>          
    </body>
</html>

UPDATE: My HTML has remained the same, and I'm posting my new PHP code. But I used a session and wrote some more. However, I've come across two problems:

1) If I refresh the page, I get an error that says that the first instance of $randomNum below the session_start(); is unidentified.

2) It seems that it remembers my very last guess in the game. If I close out the page and reopen it, I immediately get one of the messages that my guess was too high or too low, even before making a guess. Any advice is appreciated!

<?php
                    session_start();

                    $randomNum = $_SESSION['randomNum'];
                    $guess = $_POST['guessTB'];

                    if(!isset($_SESSION['randomNum']))
                    {
                        $_SESSION['randomNum'] = rand(1,1000);
                    }
                    else
                    {   
                        if($guess < $randomNum)
                        {
                            echo "<p>Your Guess is Too Low! Try Again!</p>";
                        }

                        else if($guess > $randomNum)
                        {
                            echo "<p>Your Guess is Too High! Try Again!</p>";
                        }

                        else
                        {
                            echo "<p>You Won!</p>";
                            $_SESSION = array();
                            session_destroy();
                        }
                    }           

                    echo "<p>Guess: $guess</p>";
                    echo "<p>Random Number: $randomNum</p>";
                ?>  




Aucun commentaire:

Enregistrer un commentaire