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
Share on other sites

Actually, no that's not working. The notice that the variable is undefined goes away when I first open the page, but the calculator doesn't work now as it is taking the value to always be $bmi = '' so it displays 0 whatever information I put into the calculator.

Link to comment
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 = "";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.