0

I recently started PHP and I am not able to figure out how to echo the result of this. Any help would be appreciated.

<html>
    <head>
    <style>
    input[type=submit]{
        background-color: #4CAF50;
        border: none;
        color: white;
        padding: 16px 32px;
        text-decoration: none;
        margin: 4px 2px;
        cursor: pointer;
    }
    input[type=text]{
        border: 3px solid black;
    }
    </style>
    </head>

    <body>
    <form action="fitness1.php" method="post">
    <b>weight:</b>  <input type="text" name="weight"><br>
    <br>
    <b>height:<b>   <input type="text" name="height"><br>
    <br>
    <input type="submit">
    </body>
</html>

This is the PHP code:

<html>
<body>
<?php

$WEight = $_Post['weight'];
$HEight = $_post['height'];

if (is_numeric($WEight)&& is_numeric($HEight)){

    $bmi= $WEight / $HEight * $HEight;
  }     

echo ("$bmi");


else {
    echo "please enter your weight and height";
}
?>
</body>
</html>
2
  • Parse error: syntax error, unexpected '}', expecting ',' or ';' in G:\wamp\www\test\fitness1.php on line 19 Commented Jul 3, 2016 at 7:44
  • Move the curly brace before the echo BMI line to be after that line. Commented Jul 3, 2016 at 7:45

2 Answers 2

2

The case of variables matters in PHP. You're looking for this:

$WEight = $_POST['weight'];
$HEight = $_POST['height'];

Then, this is the logic we would use:

if (is_numeric($WEight)&& is_numeric($HEight)) {
    $bmi = $WEight / $HEight * $HEight;
    echo $bmi;
} else {
    echo "please enter your weight and height";
}

Your echo needs to be inside the if (...) { ... } block.

Edit:

As requested, here's how I would improve it:

HTML:

<html>
    <head>
        <style>
        input[type=submit]{
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 16px 32px;
            text-decoration: none;
            margin: 4px 2px;
            cursor: pointer;
        }
        input[type=text]{
            border: 3px solid black;
        }
        </style>
    </head>

    <body>
        <form action="fitness1.php" method="post" />
        <b>Weight:</b> <input type="text" name="weight" /><br />
        <br />
        <b>Height:<b>  <input type="text" name="height" /><br />
        <br />
        <input type="submit" />
    </body>
</html>

PHP:

<html>
    <body>
<?php

$weightInKg = (int) $_POST['weight'];
$heightInCm = (int) $_POST['height'];

if ($heightInCm && $weightInKg) {
    $bmi = $weightInKg / pow($heightInCm, 2);
    echo "Your BMI is ${bmi}.";
} else {
    echo "Please enter your height and weight as a number greater than 0.";
}
?>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

11 Comments

No problem, glad to help. Comment if you still can't get it working.
Parse error: syntax error, unexpected 'else' (T_ELSE) in G:\wamp\www\test\fitness1.php on line 11
hey it was still giving error on line 8 and 9 i changed it to $weightInkg and $heightincm it echo bmi but with an error on line 8 of undefined variable
when i changed variable on line 8 to weightinkg and heightincm it echo's the else statement not the bmi
Oops my bad as well. Corrected :) You have to use the proper case; PHP variables are case sensitive.
|
0

Main issue of error message is :

echo("$bmi"); // this is wrong you don't need brackets.

replace this with

echo $bmi;

That's the reason of error but if would suggest to move echo $bmi inside if condition or initialise $bmi else you can get undefined variable error if it doesn't go through if condition.

5 Comments

still doesent work now its giving errors on line 5 and 6 and 10 can you write the php code for me it would be more understandable for me
undefined variable on line 5 6
fair enough you need to fix typo mistake replace line 4 and 5 ( $WEight = $_Post['weight']; $HEight = $_post['height'];) by $WEight = $_POST['weight']; $HEight = $_POST['height'];
yeah but now its not doing the calculation right like 100/2*2 =25 but its echo's 100
ok did you initialised it with 100? Can you please show your exact code what it looks like now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.