我对PHP非常陌生,过去几天我一直在接受挑战来改进我的PHP。我决定在php中构建一个BMI计算器,该计算器将根据用户对其体重和身高的输入进行计算。首先,BMI表示为体重(千克)/身高(米平方);然而,这意味着如果用户输入以磅为单位的重量和以厘米为单位的高度,则应在计算完成之前对其进行转换,这是我基于以下理解在过去3天内所做的事情:
我的PHP代码:
<?php
if(isset($_POST['calculate'])){
//once form has been submitted
$weight = $_POST['weight'];
$height= $_POST['height'];
$kilo= isset($_POST['kg'])? $_POST['kilo']:null;
$pounds= isset($_POST['pounds'])? $_POST['pounds']:null;
$cm= isset($_POST['cm'])? $_POST['cm']:null;
$meters= isset($_POST['meters'])? $_POST['meters']:null;
$meter2 = pow($meters, 2);
$bmi= $kilo / $meter2;
//check the units first
if ($kilo=='kg' && $height=='meters'){
//calculate BMI using kg/m2
return $bmi;
}
echo $bmi;
$message="success!";
}
else {
$message="Please input data";
}
?>
我的HTML代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My BMI</title>
</head>
<body>
<h1>My BMI</h1>
<form action="kd.php" method="post">
<label>Weight:
<input name="weight">
<select name="weight" id="weight">
<option selected="selected" value="">--- select one ---</option>
<option name="kg">kg</option>
<option name="pounds">pounds</option>
</select>
</label>
<p> </p>
<label>Height:
<input name="height">
<select name="height" id="height">
<option selected="selected">--- select one ---</option>
<option name="cm">cm</option>
<option name="meters">meters</option>
</select>
</label>
<p> </p>
<label>Calculate:
<input name="calculate" type="submit" value="calculate">
</label>
<p> </p>
BMI: <input readonly="true" name="bmi" value="<?php echo $bmi ?>">
</form>
<br/>
<body>
</body>
</body>
<p> <p>
<p> <p>
但它似乎没有工作:(请帮助朋友。 谢谢:))