Forum Moderators: coopster
Beginner here - whenever I show the page below I get the following
thanks,Shumit
Output:
$num_to_guess) { $message = "$guess is too big"; } elseif ($guess<$num_to_guess) { $message = "$guess is too small"; } else $message="got it"; ?>
Type your number here ___
Here is my html page:
<?php
$message="";
$num_to_guess = 42;
if (!isset($guess))
$message = "Welcome" ;
elseif ($guess>$num_to_guess)
$message = "$guess is too big";
elseif ($guess<$num_to_guess)
$message = "$guess is too small";
else
$message="got it";
?>
<html>
<head>
<title>9.9 guessing game</title>
</head>
<body>
<?php print $message ?>
<form method = "POST" >
Type your number here<input type ="text" name="guess">
</form>
</body>
</html>
I have not managed to get any php run correctly within the html.
I think that's it - your page is not being parsed for PHP - your PHP is not being executed at all! This is consistent with your original output. It's as if your file has a standard ".html" extension and not a ".php" extension, which is generally required in order to tell the webserver to parse your file for PHP.
Either that, or you are viewing your file outside of your webserver (http://localhost/) or something?
actionattribute on your form in order to tell the browser where to post back to? Something like:
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
Also, $guess will never be set if register_globals is Off (a good thing - it should be Off for security reasons and is always Off in later versions of PHP). In order to reference the value from your input element, you'll probably need to access the $_POST[] array (since method="POST"). Like:
$_POST["guess"]
<?php
$message="";
$num_to_guess = 42;
$guess=$_POST["guess"];
if (!isset($guess))
$message = "Welcome to the guessing machine";
elseif ($guess>$num_to_guess)
$message = "guess is too big";
elseif ($guess<$num_to_guess)
$message = "guess is too small";
else
$message="got it";
?>
<html>
<head>
<title>9.9 guessing game</title>
</head>
<body>
<h1>
<?php print "$message <BR>"?>
</h1>
<form method = "POST">
Type your guess here: <input type ="text" name="guess">
<br>
<!--
<input type="submit" value="hit it!">
-->
</form>
</body>
</html>
let's clarify
this is an html issue and has nothing to do with php
by default when there is no action present the form will/should post to itself
the action should be in the form tag, as should the method and a name, these things keep from too many "lack of clarity errors"
I would also suggest not using PHP_SELF. If you know the name of the script it is posting to then put it in there.
I also disagree with posting a script to itself but we can go over that again, and again, some other time