Re: [PHP] newbie issues with control structures HTML form
Jonno Agnew wrote: I wondered if I can ask you two follow-up questions? On-list, yes. Off-list, yes, at consultancy rates. (1) On Useractive's PHP environment, if ($guessnumb) { seems to work fine, but on my PHP 4.3.4 environment it doesn't. Is there some setting I need to tweak, and if so could you walk me through a way of making the change? I know register_globals is turned OFF in my set-up but don't know how to turn it on or if that's wise. http://php.net/register_globals (best to code for register_globals off). (2) Regarding your code below. I recognize these two statements as some sort of arrays. How do I actually write the equivalent code to evaluate if the variable has been passed/set, then do either this or that? if (isset($_GET['guessnumb'])) { ... } -- Stuart -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[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 input type=text name=guessnumb size=5 maxlength=3 / br / input type=hidden name=guessnumb value=?php echo $guessnumb; ? / br / input type=hidden name=tries value=?php echo $tries; ? / br / input type=submit value=guess /form /body /html ?php } else { // Since $guessnumb hasn't been passed already, set the random number $answernumb $answernumb = rand (1,101); // Set a counter for the number of tries $tries = 1; // Echo the HTML form so they can submit their answer ? html head titleThe PHP Guessing Game/title /head body Guess a number between B and 100. br / br / Make your guess: form action=guess.php method=get input type=text name=guessnumb size=5 maxlength=3 / br / input type=hidden name=guessnumb value=? echo $guessnumb; ? / br / input type=hidden name=tries value=? echo $tries; ? / br / input type=submit value=guess /form ?php // A little bit of currently escaped code to help me find errors // echo You guessed .$guessnumb. and have had .$tries. tries.; // echo The answer is .$answernumb; ? /body /html ?php } ?
Re: [PHP] newbie issues with control structures HTML form
Jonno Agnew wrote: if ($guessnumb) { Try $_GET['guessnumb'] or $_POST['guessnumb'] if the form is posted. -- Stuart -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] newbie issues with control structures HTML form
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