我在下面有这个表单代码,它工作正常,但我想知道是否有更短的方法将结果回显到 html 段落中。例如,我宁愿只说要回显的函数,而不命名构成该函数的所有变量(例如,就像在最后的 p 标记中所做的那样)。<?php//variables and resultfunction bmi($height, $weight, $waist) { $height = floatval($height); $weight = floatval($weight); $waist = floatval($waist); return round($weight / ($height * $waist),1);}?><html><head> <title>Test</title> <meta name="viewport" content="width=device-width,initial-scale=1"></head><body><form action="" method="POST"> <h4>ABI 1</h4> <input id="height" name="height" type="text" placeholder="height in meters or feet" value="<?php echo isset($_POST['height']) ? $_POST['height'] : ''; ?>" /> <br/> <br/> <input id="weight" name="weight" type="text" placeholder="weight in kgs or lbs" value="<?php echo isset($_POST['weight']) ? $_POST['weight'] : ''; ?>" /> <br/> <br/> <input id="waist" name="waist" type="text" placeholder="waist in inches or cm" value="<?php echo isset($_POST['waist']) ? $_POST['waist'] : ''; ?>" /> <br/> <br/> <input class="submit" type="submit" value="Submit"/> <?php//handles if empty or 0 inputif (!empty($_POST['height']) && !empty($_POST['weight']) && ! empty($_POST['waist'])) : ?> <p id="result">Your score is <?php echo bmi($_POST['height'], $_POST['weight'],$_POST['waist']); endif;?></p> </form></body></html>
1 回答
噜噜哒
TA贡献1784条经验 获得超7个赞
你可以试试这个
//variables and result
function bmi() {
foreach ($_POST as $name => $value) {
switch ($name) {
case 'height':
$height = floatval($value);
break;
case 'weight':
$weight = floatval($value);
break;
case 'waist':
$waist = floatval($value);
break;
}
}
return round($weight / ($height * $waist),1);
}
和
<?php
//handles if empty or 0 input
if (!empty($_POST['height']) && !empty($_POST['weight']) && !
empty($_POST['waist'])) :
?>
<p id="result">Your score is <?php echo bmi();
endif;
?>
- 1 回答
- 0 关注
- 108 浏览
添加回答
举报
0/150
提交
取消