Hello.


OK, so I'm new and I'm doing this Useractive/O'Reilly Learn PHP course and they want us to make a guessing game where you have to guess a number between 0-100 and get 10 tries; each time you're told if your guess was higher, or lower, or just right.

I've hit a wall. Can you help me (code below)?

The way the exercise set up, guess.php is supposed to print out an HTML form, call itself via POST and pass along the value $guessnumb. It's supposed to keep generating the form until either the correct answer is guessed or you've had your 10 chances to guess.

I've temporarily changed the form actions to GET to try and see what my script it doing. You'll notice that I made a couple of other changes to my code temporarily to try to help me locate and fix errors. You'll see that I have an HTML form at the front I labeled: Guess a number between A and 100, while at the back of the script the form is labeled: Guess a number between B and 100.

I also changed the form method to GET so I could see what the form was sending along in the URL; it's sending along URLs like this:

guess.php?guessnumb=21&guessnumb=&tries=1

Right now, the script is jumping to the end to form B.

I know there's an error/s in my code for the form handling as $guessnumb isn't being sent correctly by the form. I also know $tries isn't getting incremented. When I click the Submit button, my script is just printing out form B each time again without handling the submitted data.

Any tips?

Thanks,


Jonno -------------------------------------

<?php

// Check if $guessnumb has been passed before

if ($guessnumb) {


// Check the number of tries

if ($tries == 11) {

echo "Sorry game's over. The correct answer was ".$answernumb;

} else {


// Increment the number of tries by 1

$tries = $tries + 1;


// Check the passed value $guessnumb against the answer $answernumb

if ($guessnumb == $answernumb) {

echo "<i>Congratulations. You win. You are correct. Game over.</i>"."<br /><br />";

} elseif ($guessnumb > $answernumb) {

echo "<i>I'm sorry. Try again. The number you are looking for is actually lower.</i>"."<br /><br />";

} else {

echo "<i>I'm sorry. Try again. The number you are looking for is actually higher.</i>"."<br /><br />";

}

}


// Echo the HTML form so they can guess

?>
<html>
<head>
<title>The PHP Guessing Game</title>
</head>


<body>

Guess a number between A and 100.

                                                        <br />
                                                        <br />
        
                                                        Make your guess:

<form action="guess.php" method="get">

<input type="text" name="guessnumb" size="5" maxlength="3" />

<br />

<input type="hidden" name="guessnumb" value="<?php echo $guessnumb; ?>" />

<br />

<input type="hidden" name="tries" value="<?php echo $tries; ?>" />

<br />

<input type="submit" value="guess">

</form>

</body>

</html>

<?php


} else {

// Since $guessnumb hasn't been passed already, set the random number $answernumb

        $answernumb = rand (1,101);
        
        // Set a counter for the number of tries
        
        $tries = 1;
        
        // Echo the HTML form so they can submit their answer


?> <html> <head> <title>The PHP Guessing Game</title> </head>

<body>

Guess a number between B and 100.

                        <br />
                        <br />

Make your guess:

<form action="guess.php" method="get">

<input type="text" name="guessnumb" size="5" maxlength="3" />

<br />

<input type="hidden" name="guessnumb" value="<? echo $guessnumb; ?>" />

<br />

<input type="hidden" name="tries" value="<? echo $tries; ?>" />

<br />


<input type="submit" value="guess">

                                </form>
                        
                        <?php
                
                // A little bit of currently escaped code to help me find errors
                
                //      echo "You guessed ".$guessnumb." and have had ".$tries." 
tries.";
                //      echo "The answer is ".$answernumb;
                        
                        ?>
                        
                        </body>
                        
                </html>

<?php

}

?>

Reply via email to