PHP doesn't preform calucation -
i've php form user enters height , weight , once click submit info resubmitted form , calucation bmi should excute , display result. i'm little unsure how code , i've checked errors phpcodechecker.com found none, don't result. i'm unsure if calculation happening @ all. on how calculate bmi off info user enters appreciated.
the forumal need use
bmi = (weight in pounds * 703) / (height in inches) squared note: there 12 inches foot so, if person weighs 150 pounds , 6 feet tall, calculation bmi = (150 * 703) / 72 x 72 (6 feet = 72 inches, squared)
code below:
<form method="post" action="<?php echo $_server['php_self']; ?>"> <label for="weight">enter weight in pounds:</label><br> <input id="weight" name="weight" type="text" value="<?php echo $weight; ?>" size="30"><br> <label for="height">enter height in inches:</label><br> <input id="height" name="height" type="text" value="<?php echo $height; ?>" size="30"><br> <input type="submit" name="submit" value="calculate"> </form> <?php } ?> <?php $weight = ''; $height = ''; if (isset($_post['submit'])) { $weight = $_post['weight']; $height = $_post['height']; $output_form = false; if (empty($weight) && empty($height)) { echo 'you forgot enter weight.<br />'; $output_form = true; } if (empty($weight) && (!empty($height))) { echo 'you forgot enter height.<br />'; $output_form = true; } } else { $output_form = true; } if ((!empty($weight)) && (!empty($height))) { $string = "(weight * 703)/height * height"; eval($string); echo($res); ?> <form method="post" action="<?php echo $_server['php_self']; ?>"> <label for="weight">enter weight in pounds:</label><br> <input id="weight" name="weight" type="text" value="<?php echo $weight; ?>" size="30"><br> <label for="height">enter height in inches:</label><br> <input id="height" name="height" type="text" value="<?php echo $height; ?>" size="30"><br> <input type="submit" name="submit" value="calculate"> </form> <?php } ?>
some tips:
- avoid using eval in code. slow , insecure.
- it seems have same form 2 times (repeated code). also, try avoid this. changed code don't have repeat it
a better working code may this:
<?php $weight = ''; $height = ''; $output_form = true; if (isset($_post['submit'])) { $weight = $_post['weight']; $height = $_post['height']; if (empty($weight) && empty($height)) { //test both echo 'you forgot enter weight , height.<br>'; }else if (empty($weight)) { echo 'you forgot enter weight.<br />'; }else if (empty($height)) { echo 'you forgot enter height.<br />'; }else{ //if none empty, can proceed calc $output_form = false; //everything ok, hide form $res = ($weight * 703)/($height * $height); echo $res; } } if($output_form) { ?> <form method="post" action="<?php echo $_server['php_self']; ?>"> <label for="weight">enter weight in pounds:</label><br> <input id="weight" name="weight" type="text" value="<?php echo $weight; ?>" size="30"><br> <label for="height">enter height in inches:</label><br> <input id="height" name="height" type="text" value="<?php echo $height; ?>" size="30"><br> <input type="submit" name="submit" value="calculate"> </form> <?php }
Comments
Post a Comment