Problem with PHP form input
Posted: Fri Jul 02, 2010 10:16 pm
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'].
my learning stuck with this issue. please help me asap.
thanks a ton in advance...
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>thanks a ton in advance...