我是PHP新手,我正在做一些简单的练习,试图完全理解我学到的每一件事.我自信地输出从表单输入的数据,但对于这个特殊的练习,我看不出我错在哪里.表单当前正在输出"1".

我正在try 建立一个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;
} 
?>

推荐答案

源代码中唯一缺少的部分是使用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公斤重进行测试时的输出:

enter image description here

如果输入为167和90,而不是1.67和90,则会得到以下结果:

Your bmi is 0.0032270787765786

四舍五入为1...

体重指数是利用一个人的身高和体重进行的简单计算.公式为BMI=千克/平方米,其中千克是一个人的体重,单位为千克,平方米是他们的身高,单位为平方米

因此,1.67米比167米更好,才能获得好的结果;)

Php相关问答推荐

WooCommerce拆分运输包裹上的商品数量增加运输成本

在AJX请求中使用简写后,FormData出现非法调用错误

在PHP中替换数组中的文本并同时使用其他函数

从含有特殊字符的ANSI文本文件中读取数据是在移值,为什么?

将变体设置字段添加到WooCommerce中的特定变量产品

无法在WooCommerce中发送客户笔记邮箱通知

在WooCommerce中添加基于类别的固定费用

Laravel:测试命令时无法获取工作通知::AssertSentTo

PHP MySQL求和子树并与父树累加

如何实现对数据库表中多列的唯一约束?

免费送货柜台,考虑到折扣码

在WooCommerce管理中 for each 运输方法设置添加自定义字段

哪里的表现能更好?

woocommerce_available_ payment_gateways 过滤器挂钩多次触发

订单完成后创建 WooCommerce 促销代码

在php中计算两个没有假期的日期之间的小时差

根据页面的最后修订日期设置 MediaWiki 内部链接的样式

使用 RSA 2048 公钥验证 RSA PKCS#1v1.5 SHA 256 签名

遇到特定键时修改二维数组以创建嵌套数据集

如何在 Laravel 中保存多对多