Hi,
I have a book that I've been using to learn PHP and MySQL.
In it, it uses some code to show you how to use hidden fields which is not
working correctly.
The code I'm using is:
************************************************** ***
<?php
// The number of guesses is either 0 or $num_tries + 1
$num_tries = (isset($_POST[num_tries])) ? $num_tries + 1 : 0;
// The number to guess
$num_to_guess = 42;
// The message to display
$message = "";
// Now we need to check the guess value
// We start by seeing if there is any value in the guess value
if (!isset($_POST[guess])){
$message = "Welcome to the guessing Machine!";
} elseif ($_POST[guess] > $num_to_guess ){
$message = "$_POST[guess] is to big! Try a smaller number";
} elseif ($_POST[guess] < $num_to_guess ) {
$message = "$_POST[guess] is to small! Try a bigger number";
} else {
$message = "Well Done!";
}
// We are placing the Posted Guess value into a holder called $guess
$guess = $_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 ?>
<?php print "\n" ?>
<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>
************************************************** ***************
The problem I'm having is to do with the hidden value. It increments from 0
to 1 fine,
but then it won't increment it any further. It stays at 1.
I have also tried replacing the code:
"
// The number of guesses is either 0 or $num_tries + 1
$num_tries = (isset($_POST[num_tries])) ? $num_tries + 1 : 0;
"
with
"
// The number of guesses is either 0 or $num_tries + 1
if (isset($_POST[num_tries])) {
$num_tries = $num_tries + 1;
} else {
$num_tries = 0;
}
"
with no luck either....
Any suggestions?