php 回显到新的 html 部分/标签

我在下面有这个表单代码,它工作正常,但我想知道是否有更短的方法将结果回显到 html 段落中。例如,我宁愿只说要回显的函数,而不命名构成该函数的所有变量(例如,就像在最后的 p 标记中所做的那样)。


<?php

//variables and result

function 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 input

if (!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>


白板的微信
浏览 111回答 1
1回答

噜噜哒

你可以试试这个//variables and resultfunction bmi() {&nbsp; &nbsp; foreach ($_POST as $name => $value) {&nbsp; &nbsp; &nbsp; &nbsp; switch ($name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'height':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $height = floatval($value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'weight':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $weight = floatval($value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'waist':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $waist = floatval($value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return round($weight / ($height * $waist),1);}和<?php//handles if empty or 0 inputif (!empty($_POST['height']) && !empty($_POST['weight']) && !&nbsp;empty($_POST['waist'])) :&nbsp;?>&nbsp;<p id="result">Your score is <?php echo bmi();&nbsp;endif;?>
打开App,查看更多内容
随时随地看视频慕课网APP