Page 1 of 1

[Solved]-what's wrong in this Code?

Posted: Thu Jul 21, 2011 8:13 am
by newbie-in-php
Hi all
I'm newbie in php.
Can anyone tell me why this code isn't working? I know i am missing something just cant figure out what. I am trying to test a form that sent a variable through a form to a php scipt like this:

Code: Select all

<html>
<head><title>ageist</title></head>
<body>
            <form action="ageist.php" method="post">
                 Enter Your Age: <input name="age" size="2">  <br />
           <input type="submit" value="send">

          </form>
</body>
</html>
but the php script does not work properly. indeed the first curly braces not work! In other words, when i enter $age = 21 or a number greater 21, the code within FCB (First Curly Braces) is not executed. i see a WhitePage. On the other hand when inter a number less than 21 the seconf Curly Braces work fine and executed. seems the combination operators >= not work fine but i don't know why!!!?
THE PHP SCRIPT:

Code: Select all

<html>
<head></head>
<body>

<?php
// retrieve form data
$age = $_POST['age'];
// check entered value and branch
if ($age >= 21) {
     echo 'Come on in, we have alcohol and music awaiting you!';
}
if ($age < 21) {
     echo "You're too young for this club, come back when you're a little older";
}
?>

</body>
</html> 
(It should be noted that, codes are a copy paste from here: Older But Not Wiser and i'm doing this for php-learning ...)
If anyone knows whats wrong could you please explain. thanks

Re: what's wrong in this Code?

Posted: Thu Jul 21, 2011 9:20 am
by social_experiment
Try an if-else statement.

Code: Select all

<?php
if ($age < 21)
{
 echo "You're too young...";
}
else
{
 echo "Come on in...";
}
?>

Re: what's wrong in this Code?

Posted: Thu Jul 21, 2011 11:37 am
by Weirdan
There's nothing wrong with the code you posted. Check webserver logs to see if there were any errors though.

Re: what's wrong in this Code?

Posted: Fri Jul 22, 2011 1:43 am
by newbie-in-php
@ (Weirdan & social_experiment)
Thank you so much for responding so quickly.
Weirdan, you mentioned a good point :-)

This is the Apache2 logs on /var/log/apache2/error.log
......................................
[Fri Jul 22 05:47:18 2011] [error] [client 127.0.0.1] PHP Notice: Undefined variable: age in /var/www/php-test/ageist.php on line 13, referer: http://localhost/php-test/ageist.html
[Fri Jul 22 05:47:18 2011] [error] [client 127.0.0.1] PHP Notice: Undefined variable: age in /var/www/php-test/ageist.php on line 16, referer: http://localhost/php-test/ageist.html
.....................................
It Was My Fault! on line 9
$sgae = $_POST['age'];

$age = $_POST['age']; (This is corrected)

Thank you in advance for your time and response.