Jump to content

BMI Calculation in php


mglover88

Recommended Posts

Hi all,

 

I need to add a BMI (Body Mass Index) calculator to my site. The calculator works but when the page is first opened (before any info has been put into the calculator) the page displays the message "Notice: Undefined variable: bmi in C:\wamp\www\bmiCalculator.php on line 242" where the final calculation will be displayed.

 

I was just wondering if anyone knows how to fix this or if it's anything at all to do with the fact I am using it on a testing server at the moment?

 

The code I am using to power the equation is as follows:

<?php
if ((isset($_POST["done"])) && ($_POST["done"] =="y")){

$height = ((12 * $_POST["feet"]));
if ((isset($_POST["inches"])) && ($_POST["inches"] > 0)){
$height = $height + $_POST["inches"];
}

$bmi = ($_POST["lbs"]) / ($height * $height) * 703;

}
?>

 

And the actual calculator is in a table as follows:

<form method="post" name="bmiForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
  ...
    <tr>
      <td><label>
      <input type="submit" name="Submit" value="Calculate" tabindex="40" />
      <input name="done" type="hidden" value="y" />
  ...
    <tr>
      <td>your BMI is:</td>
      <td class="bold"><?php echo round($bmi,1); ?></td>
      <td> </td>
    </tr>
  </table>
  </form>

 

Thanks in advance for any help.

 

 

Link to comment
https://forums.phpfreaks.com/topic/240306-bmi-calculation-in-php/
Share on other sites

add $bmi=""; AFTER as the second condition of your IF statement...

 

<?php
<?php
if ((isset($_POST["done"])) && ($_POST["done"] =="y")){

$height = ((12 * $_POST["feet"]));
if ((isset($_POST["inches"])) && ($_POST["inches"] > 0)){
$height = $height + $_POST["inches"];
}

$bmi = ($_POST["lbs"]) / ($height * $height) * 703;

}else{
$bmi = "";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.