Problem with PHP form input

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
duvara
Forum Newbie
Posts: 3
Joined: Fri Jul 02, 2010 10:04 pm

Problem with PHP form input

Post 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...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with PHP form input

Post by requinix »

duvara wrote:can we fix this without using $_POST['guess'] ?
No.


And I will shoot anybody who mentions That One Setting.
duvara
Forum Newbie
Posts: 3
Joined: Fri Jul 02, 2010 10:04 pm

Re: Problem with PHP form input

Post 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..
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with PHP form input

Post by requinix »

It's probably too old. If it isn't then it's teaching you bad practices.

Try... I dunno, O'Reilly?
duvara
Forum Newbie
Posts: 3
Joined: Fri Jul 02, 2010 10:04 pm

Re: Problem with PHP form input

Post by duvara »

thank u very much ur guidance..
i'll search for some new material then...
Post Reply