Page 1 of 1

Question!

Posted: Sun Oct 10, 2004 10:37 am
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

Posted: Sun Oct 10, 2004 10:53 am
by feyd
uh.. $_POST['num_tries']

Posted: Sun Oct 10, 2004 10:55 am
by John Cartwright
The notice means you are using a variable before you have set it...