Question!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
loongest
Forum Commoner
Posts: 25
Joined: Wed Apr 07, 2004 12:23 pm

Question!

Post by loongest »

Code: Select all

<?php
$num_to_guess = 42;
$num_tries = (isset($_POST['num_tries'])) ? (int)$num_tries  + 1 : 0;
$message = "";

if (!isset($_POST['guess']))
{
   $message = "Welcome to the guessing machine!";
}
elseif ($_POST['guess'] > $num_to_guess )
{
   $message = "$_POST[guess] is too big! Try a smaller number";
}
elseif ($_POST['guess'] < $num_to_guess )
{
   $message = "$_POST[guess] is too small! Try a larger number";
} 
else
{  // must be equivalent
   $message = "Well done!";
}

$guess = (int) $_POST['guess'];
?>

<html>
<head>
<title>Listing 9.8 Saving state with a hidden field</title>
</head>

<body>

<h1>
<?php print $message ?>
</h1>

Guess number: <?php print $num_tries?>

<form action="<?php print $_SERVER['PHP_SELF'] ?>" method="POST">

	Type your guess here:
	<input type="text" name="guess" value="<?php print $guess?>">
	<input type="hidden" name="num_tries" value="<?php print $num_tries?>">
</form>
</body>
</html>

This is the example suppose to call from same page .
what's the problem with this coding ? it keep showing the error message after i press enter. Can anyone help pls ?
Notice: Undefined variable: num_tries in C:\Mywork\new\test.php on line 3
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uh.. $_POST['num_tries']
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

The notice means you are using a variable before you have set it...
Post Reply