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.Sorry; is this a PHP5 question or a PHP4.3 question?
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'];
If $_POST['guess'] is not set (i.e., that field was empty); then the message will be set to "Welcome to the guessing machine!".
The problem is, even if it wasn't set, the code still tries to use it later on (the line $guess=$_POST['guess']; ). That's when the error will happen. Considering that $guess is used later to repopulate the form field, a reasonable fix would be to add a line immediately after the "Welcome to the guessing machine!" line to make sure $_POST['guess'] is set with an empty value:
$_POST['guess']="";I'm using PHP 5. That worked to clear up the initial problem but once you enter info into the text field and click submit
then the other one is called variable is called and it produces a similiar error. But I would think that it would have
already triggered an error at the beginning of the file.
The error I get now is:
PHP Notice: Undefined variable: num_tries in c:\inetpub\wwwroot\php\listing9.7.php on line 4
This is how the new file looks after I've added your change. I'm guessing that I need to do the same thing only figure out
exactly were to put the num_tries. I thought that it set it at the top of the script. Also, thanks for the input. Do you
know if this was just bad coding or something different from PHP 4 to PHP 5, and if so, do you know where it might be
covered in the php manual. Haha, sorry to ask so much. Well again thanks for the help.
<?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!";
$_POST['guess']="";
}
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>Ask this in the coding forum... you'll get more eyeballs there than here.
<?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.Sorry; is this a PHP5 question or a PHP4.3 question?
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'];
If $_POST['guess'] is not set (i.e., that field was empty); then the message will be set to "Welcome to the guessing machine!".
The problem is, even if it wasn't set, the code still tries to use it later on (the line $guess=$_POST['guess']; ). That's when the error will happen. Considering that $guess is used later to repopulate the form field, a reasonable fix would be to add a line immediately after the "Welcome to the guessing machine!" line to make sure $_POST['guess'] is set with an empty value:
$_POST['guess']="";I'm using PHP 5. That worked to clear up the initial problem but once you enter info into the text field and click submit
then the other one is called variable is called and it produces a similiar error. But I would think that it would have
already triggered an error at the beginning of the file.
The error I get now is:
PHP Notice: Undefined variable: num_tries in c:\inetpub\wwwroot\php\listing9.7.php on line 4
This is how the new file looks after I've added your change. I'm guessing that I need to do the same thing only figure out
exactly were to put the num_tries. I thought that it set it at the top of the script. Also, thanks for the input. Do you
know if this was just bad coding or something different from PHP 4 to PHP 5, and if so, do you know where it might be
covered in the php manual. Haha, sorry to ask so much. Well again thanks for the help.
<?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!";
$_POST['guess']="";
}
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>Ask this in the coding forum... you'll get more eyeballs there than here.