Mr66
09-03-2004, 05:50 AM
Hello, I'm learning PHP from a book teaching PHP 4.3 and I've ran into a problem I can't solve.
<?php
$num_to_guess = 42;
$num_tries = (isset($_POST['num_tries'])) ? $num_tries + 1 : 0;
$message = "";
if (!isset($_POST['guess']))
{
$message = "Welcome to the guessing machine!";
}
else if ($_POST['guess'] > $num_to_guess)
{
$message = $_POST['guess'] . " is to big! Try a smaller number";
}
else if ($_POST['guess'] < $num_to_guess)
{
$message = $_POST['guess'] . " is too small! Try a larger number";
}
else
{ // must be equivalent
$message = "Well done! " . $_POST['guess'] . " is correct";
}
$guess = $_POST['guess'];
?>
<html>
<head>
<title>
Listing 9.6 A PHP number guessing script
</title>
</head>
<body>
<h1>
<?php echo $message ?>
</h1>
<p><strong>Guess Number:</strong> <?php echo $num_tries ?></p>
<form action= "<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<p><strong>Type your guess here:</strong>
<input type="text" name="guess" value="<?php echo $guess ?>">
<input type="hidden" name="num_tries" value="<?php echo $num_tries ?>">
</p>
<p><input type="submit" value="submit your guess"></p>
</form>
</body>
</html>
The error I receive when I open the page
PHP Notice: Undefined index: guess in c:\inetpub\wwwroot\php\listing9.7.php on line 25
I know if has something to do with the <?php echo $guess ?> and <?php echo $num_tries ?> in the html part. But can't figure it out. Anyone have any ideas what I'm doing wrong.
<?php
$num_to_guess = 42;
$num_tries = (isset($_POST['num_tries'])) ? $num_tries + 1 : 0;
$message = "";
if (!isset($_POST['guess']))
{
$message = "Welcome to the guessing machine!";
}
else if ($_POST['guess'] > $num_to_guess)
{
$message = $_POST['guess'] . " is to big! Try a smaller number";
}
else if ($_POST['guess'] < $num_to_guess)
{
$message = $_POST['guess'] . " is too small! Try a larger number";
}
else
{ // must be equivalent
$message = "Well done! " . $_POST['guess'] . " is correct";
}
$guess = $_POST['guess'];
?>
<html>
<head>
<title>
Listing 9.6 A PHP number guessing script
</title>
</head>
<body>
<h1>
<?php echo $message ?>
</h1>
<p><strong>Guess Number:</strong> <?php echo $num_tries ?></p>
<form action= "<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<p><strong>Type your guess here:</strong>
<input type="text" name="guess" value="<?php echo $guess ?>">
<input type="hidden" name="num_tries" value="<?php echo $num_tries ?>">
</p>
<p><input type="submit" value="submit your guess"></p>
</form>
</body>
</html>
The error I receive when I open the page
PHP Notice: Undefined index: guess in c:\inetpub\wwwroot\php\listing9.7.php on line 25
I know if has something to do with the <?php echo $guess ?> and <?php echo $num_tries ?> in the html part. But can't figure it out. Anyone have any ideas what I'm doing wrong.