Looking at the URL:
guess.php?guessnumb=21guessnumb=tries=1
I notice that 'guessnumb' is in there twice.
PHP will use the last one in that list
I notice that in your form, you have two inputs with the same name
input type=text name=guessnumb size=5 maxlength=3 /
input type=hidden name=guessnumb value=?php echo $guessnumb; ?
/
change one of these to something else and that should do the trick
HTH
Martin
-Original Message-
From: Jonno Agnew [mailto:[EMAIL PROTECTED]
Sent: Friday, 23 January 2004 7:16 AM
To: [EMAIL PROTECTED]
Subject: [PHP] newbie issues with control structures HTML form
Hello.
OK, so I'm new and I'm doing this Useractive/O'Reilly Learn
PHP course
and they want us to make a guessing game where you have to guess a
number between 0-100 and get 10 tries; each time you're told if your
guess was higher, or lower, or just right.
I've hit a wall. Can you help me (code below)?
The way the exercise set up, guess.php is supposed to print
out an HTML
form, call itself via POST and pass along the value $guessnumb. It's
supposed to keep generating the form until either the correct
answer is
guessed or you've had your 10 chances to guess.
I've temporarily changed the form actions to GET to try and
see what my
script it doing. You'll notice that I made a couple of other
changes to
my code temporarily to try to help me locate and fix errors.
You'll see
that I have an HTML form at the front I labeled: Guess a
number between
A and 100, while at the back of the script the form is
labeled: Guess a
number between B and 100.
I also changed the form method to GET so I could see what the
form was
sending along in the URL; it's sending along URLs like this:
guess.php?guessnumb=21guessnumb=tries=1
Right now, the script is jumping to the end to form B.
I know there's an error/s in my code for the form handling as
$guessnumb isn't being sent correctly by the form. I also know $tries
isn't getting incremented. When I click the Submit button, my
script is
just printing out form B each time again without handling the
submitted
data.
Any tips?
Thanks,
Jonno
-
?php
// Check if $guessnumb has been passed before
if ($guessnumb) {
// Check the number of tries
if ($tries == 11) {
echo Sorry game's
over. The correct answer was .$answernumb;
} else {
// Increment
the number of tries by 1
$tries = $tries + 1;
// Check the
passed value $guessnumb against the answer
$answernumb
if ($guessnumb
== $answernumb) {
echo
iCongratulations. You win. You are correct. Game
over./i.br /br /;
} elseif
($guessnumb $answernumb) {
echo iI'm
sorry. Try again. The number you are looking for is
actually lower./i.br /br /;
} else {
echo iI'm
sorry. Try again. The number you are looking for is
actually higher./i.br /br /;
}
}
// Echo the HTML form
so they can guess
?
html
head
titleThe PHP Guessing Game/title
/head
body
Guess a
number between A and 100.
br /
br /
Make your guess:
form action=guess.php method=get