我是 PHP 新手,我正在做一些簡單的練習來嘗試完全理解我學到的每一件事。我自信地輸出從表單輸入的資料,但對于這個特定的練習,我看不出我哪里出錯了。表單當前正在輸出“1”。
我正在嘗試構建一個 BMI 計算器,感謝您的幫助,在此先感謝您。
我的表格
<?php
return "
<p>Calculate your BMI</p>
<form method='post' action='index.php?page=process-bmi'>
<p>Please enter your height and weight to calculate your BMI</p>
<label for='height'>Height</label>
<input type='text' id='height' name='height'>
<label for='weight'>Weight</label>
<input type='text' id='weight' name='weight'>
<input type='submit' value'Submit' name='bmi-submitted'>
</form>";
我的PHP是
<?php
$bmiSubmitted = isset($_POST['bmi-submitted']);
if ($bmiSubmitted) {
$weight = $_POST['weight'];
$height = $_POST['height'];
$output = bmi($weight, $height);
}
function bmi($weight, $height) {
$bmi = $weight/($height*$height);
$response = "<p>Your bmi is $bmi</p>";
return $response;
}
?>
uj5u.com熱心網友回復:
源代碼中唯一缺少的部分是使用 echo 陳述句顯示結果:)
這是您在一個尋呼機中的代碼:
<?php
$bmiSubmitted = isset($_POST['bmi-submitted']);
if ($bmiSubmitted) {
$weight = $_POST['weight'];
$height = $_POST['height'];
$output = bmi($weight, $height);
}
function bmi($weight, $height) {
$bmi = $weight/($height*$height);
$response = "<p>Your bmi is $bmi</p>";
var_dump($response);
return $response;
}
?>
<p>Calculate your BMI</p>
<form method='post' action='#'>
<p>Please enter your height and weight to calculate your BMI</p>
<label for='height'>Height</label>
<input type='text' id='height' name='height'>
<label for='weight'>Weight</label>
<input type='text' id='weight' name='weight'>
<input type='submit' value'Submit' name='bmi-submitted'>
</form>
這是我在身高 167 厘米、體重 90 公斤的情況下進行測驗時的輸出:

如果輸入是 167 和 90 而不是 1.67 和 90,您會得到以下結果:
Your bmi is 0.0032270787765786
四舍五入到 1...
體重指數是使用人的身高和體重進行的簡單計算。公式是 BMI = kg/m2,其中 kg 是一個人的體重,以公斤為單位,m2 是他們的身高,以米為單位的平方
所以 1.67 米比 167 米要好 ;)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/496457.html
