Reputation: 23
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>
Upvotes: 0
Views: 46
Reputation: 24699
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>
Upvotes: 2
Reputation: 492
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.
Upvotes: 0