Forum Moderators: coopster

Message Too Old, No Replies

another simple problem - promise not a typo

php and html example

         

shumboom

9:03 pm on Apr 14, 2008 (gmt 0)

10+ Year Member



Hi

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>

Demaestro

9:14 pm on Apr 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the > in the line below is closing the <?php> tag

elseif ($guess>$num_to_guess)

That is why it starts with $num_to_guess) and prints everything after.

Demaestro

9:16 pm on Apr 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



oh and your if else block is malformed.

it should be

if (this = that)
{do this}
else (this != that)
{do that}

You are missing your curly brackets.

jatar_k

9:22 pm on Apr 14, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you don't need curly braces if you only have a single line

shumboom

11:34 pm on Apr 14, 2008 (gmt 0)

10+ Year Member



thanks again! Ive changed the offending bracket to a '<' But what if I wanted to use > ?

jatar_k

11:38 pm on Apr 14, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can

I actually pasted your code as is and it showed the form just fine, not really sure why it cut out on you

I added a submit button and a line to pull in the posted var and the whole thing worked fine

shumboom

6:15 am on Apr 15, 2008 (gmt 0)

10+ Year Member



Could it be EasyPhp's setup?. For instance I cannot see the result of

<?php print $message ?>

in the page. in fact I have not managed to get any php run correctly within the html.

penders

12:21 pm on Apr 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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?

shumboom

9:48 am on Apr 17, 2008 (gmt 0)

10+ Year Member



I have installed php5 now and tried the code and it half works - I keep getting the Welcome message but none of the comparisons are ever tested- it is as if my $guess is never set.

Is this a globals issue?

penders

10:44 am on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you might need to post your revised code...? As there a number of issues with your original code as is. For instance, do you have an
action
attribute on your form in order to tell the browser where to post back to? Something like:
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">

(if posting back to the same page)

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"]

shumboom

12:37 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



Many thanks - the $_POST was the solution - here's the final listing. BTW the original listing is from the SAMS teach yourself PHP book so I think it needs updating.

<?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>

wheelie34

12:59 pm on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you forgot the action attribute again

<form method = "POST">

Should be

<form method ="POST" action="<?=$_SERVER["PHP_SELF"]?>">

As suggested above by penders

shumboom

1:09 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



actually I didnt - the line gave an error in easyphp but have tried it now and is working ok in php5.

jatar_k

1:27 pm on Apr 17, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> you forgot the action attribute again

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