<?php
include('../includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
echo('<h1>Body Mass Index Calculator</h1>');
// variables passed via the POST ternary operators
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$height = isset($_POST['height']) ? $_POST['height'] : '';
$weight = isset($_POST['weight']) ? $_POST['weight'] : '';
// filter input taken from browser
$step = htmlentities($step);
$height = htmlentities($height);
$weight = htmlentities($weight);
// first time viewing form
if ($step == 0) {
?>
<form action="bmi-calc.php" method="post">
<label for="height">Height (in inches):</label>
<input type="text" size="36" id="height" name="height" value="">
<br><br>
<label for="weight">Weight (in pounds):</label>
<input type="text" size="36" id="weight" name="weight" value="">
<br><br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
else //else shows what the form gathered
{
echo('<p>The following is what the form gathered</p>');
echo('<p>Please note: The POST array has not been scrubbed</p>');
echo('<p>Thus, it is open to javascript insertion.</p>');
echo('<pre>');
print_r($_POST); // dump contents of the $_POST array
echo('</pre>');
if ($height > 0 && $weight > 0) {
$bmi = ($weight / ($height * $height)) * 703;
$bmi = round($bmi, 1);
echo("<h3>BMI = $bmi</h3>");
if ($bmi < 18.5)
{
echo('<p>Category: Underweight</p>');
}
else if ($bmi >= 18.5 && $bmi < 25)
{
echo('<p>Category: Normal</p>');
}
else if ($bmi >= 25 && $bmi < 30)
{
echo('<p>Category: Overweight</p>');
}
else
{
echo('<p>Category: Obese</p>');
}
}
else
{
echo('<p>Invalid input provided.</p>');
}
// provide a form to try again
?>
<form action="bmi-calc.php" method="post">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include('../includes/footer.php');
?>