mglover88 Posted June 24, 2011 Share Posted June 24, 2011 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. Quote Link to comment Share on other sites More sharing options...
AMcHarg Posted June 24, 2011 Share Posted June 24, 2011 Just define $bmi at the top of your page: $bmi = ''; An undefined variable is a variable that you are trying to use but hasn't been given a value. Quote Link to comment Share on other sites More sharing options...
mglover88 Posted June 24, 2011 Author Share Posted June 24, 2011 Awesome. Thanks very much, it's always something simple that I miss. Quote Link to comment Share on other sites More sharing options...
mglover88 Posted June 24, 2011 Author Share Posted June 24, 2011 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. Quote Link to comment Share on other sites More sharing options...
micah1701 Posted June 24, 2011 Share Posted June 24, 2011 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 = ""; } ?> Quote Link to comment Share on other sites More sharing options...
mglover88 Posted June 24, 2011 Author Share Posted June 24, 2011 Ah ok , that's definitely working fine now. Thanks a lot, really appreciate it. Quote Link to comment Share on other sites More sharing options...
colap Posted June 24, 2011 Share Posted June 24, 2011 Just define $bmi at the top of your page: $bmi = ''; An undefined variable is a variable that you are trying to use but hasn't been given a value. Why is it necessary to define $bmi="" ; at top? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.