我有一个php表单,用户输入他们的身高和体重,一旦他们点击提交,信息将重新提交到表单中,并且bmi的calucation应该执行并显示结果。我有点不确定如何使用我的代码执行此操作并且我已经检查了错误phpcodechecker.com找不到,但我没有得到结果。所以我不确定计算是否真的发生了。任何关于如何计算用户输入信息的BMI的帮助都将不胜感激。
我需要使用的是
BMI =(以磅为单位的重量* 703)/(以英寸为单位的高度)的平方 注意:有12英寸到一英尺 因此,如果一个人体重150磅并且身高6英尺,那么计算就是 BMI =(150 * 703)/ 72 x 72(6英尺= 72英寸,然后是平方)
以下代码:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="weight">Enter your weight in pounds:</label><br>
<input id="weight" name="weight" type="text" value="<?php echo $weight; ?>" size="30"><br>
<label for="height">Enter your 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 to enter your weight.<br />';
$output_form = true;
}
if (empty($weight) && (!empty($height))) {
echo 'You forgot to enter your 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 your weight in pounds:</label><br>
<input id="weight" name="weight" type="text" value="<?php echo $weight; ?>" size="30"><br>
<label for="height">Enter your 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
}
?>
答案 0 :(得分:2)
这看起来不对:
$string = "(weight * 703)/height * height";
eval($string);
echo($res);
$res
变量; 你应该只计算你需要的值并将其反映出来:
echo ($weight * 703)/$height * $height;
除此之外你没有使用$output_form
变量,但我猜你还没有添加它。
答案 1 :(得分:2)
一些提示:
更好的工作代码可能如下所示:
<?php
$weight = '';
$height = '';
$output_form = true;
if (isset($_POST['submit'])) {
$weight = $_POST['weight'];
$height = $_POST['height'];
if (empty($weight) && empty($height)) { //test for both
echo 'You forgot to enter your weight and height.<br>';
}else if (empty($weight)) {
echo 'You forgot to enter your weight.<br />';
}else if (empty($height)) {
echo 'You forgot to enter your height.<br />';
}else{ //if none is empty, you can proceed with calc
$output_form = false; //everything ok, hide the form
$res = ($weight * 703)/($height * $height);
echo $res;
}
}
if($output_form) { ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="weight">Enter your weight in pounds:</label><br>
<input id="weight" name="weight" type="text" value="<?php echo $weight; ?>" size="30"><br>
<label for="height">Enter your 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 }
答案 2 :(得分:0)
您走在正确的轨道上,只需执行以下操作:
if ((!empty($weight)) && (!empty($height))) {
$result = ($weight * 703) / $height * $height;
echo $result;
// ...