Page 1 of 1

Problem with PHP form input

Posted: Fri Jul 02, 2010 10:16 pm
by duvara
dear frnz..
im learning php.. i'm having the problem with the below code.. i'm not getting any error. the page is always showing the message "Welcome to the guessing machine!". it means the condition if ( ! isset( $guess ) ) is always true. the given value in the html form is not moved to the php varable $guess. can we fix this without using $_POST['guess'] ? also i got the error with PHP_SELF. so ive changed to $_SERVER['PH_SELF'].

Code: Select all

<?php


 $num_to_guess = 42;

 $message = "";

 if ( ! isset( $guess ) )
 {
 $message = "Welcome to the guessing machine!";
 }
 elseif ( $guess > $num_to_guess )
 {
 $message = "$guess is too big! Try a smaller number";
 }
 elseif ( $guess < $num_to_guess )
 {
 $message = "$guess is too small! Try a larger number";
 }
 else // must be equivalent
 {
 $message = "Well done!";
 }
// $guess = (int) $guess;
 ?>
 <html>
 <head>
 <title>Listing 9.10 A PHP number guessing script</title>
 </head>
 <body>
 <h1>
 <?php print $message ?>
 </h1>

 <form action="<?php print $_SERVER['PHP_SELF']?>" method="POST">
 Type your guess here: <input type="text" name="guess">
 </form>
 </body>
 </html>
my learning stuck with this issue. please help me asap.
thanks a ton in advance...

Re: Problem with PHP form input

Posted: Fri Jul 02, 2010 11:41 pm
by requinix
duvara wrote:can we fix this without using $_POST['guess'] ?
No.


And I will shoot anybody who mentions That One Setting.

Re: Problem with PHP form input

Posted: Sat Jul 03, 2010 12:05 am
by duvara
thanks for the reply..
actually i've found this code in a php learning material(SAMSTeachYourselfPHP4in24Hours.pdf). i'm wondering how come that can have the example code which won't work!!!!! i dont know whether i can continue with that pdf..

Re: Problem with PHP form input

Posted: Sat Jul 03, 2010 1:00 am
by requinix
It's probably too old. If it isn't then it's teaching you bad practices.

Try... I dunno, O'Reilly?

Re: Problem with PHP form input

Posted: Sat Jul 03, 2010 4:23 am
by duvara
thank u very much ur guidance..
i'll search for some new material then...