将int转换为英寸并将结果显示为回显

时间:2015-06-22 22:35:57

标签: php

这里我们从dropDownMenu中选择一个性别,然后输入一个高度。这个高度需要转换成英寸。由于某种原因,最终结果不会显示当前代码。我认为问题可能在于我试图回应$ result变量的方式。

背景:女人的理想体重是通过将她的身高乘以英寸3.5乘以减去108来得到的。男人的理想体重是通过将他的身高乘以4英寸并减去128而得到的。

我需要找到一种简单的方法来显示结果,无论是回声还是显示在文本框中。有没有人有任何想法?

<?php
if(isset($_POST['submit'])) {

$gender = isset($_POST['gender']) ? $_POST['gender']: 0;
$height = (int)$_POST['height'];

switch ($gender) {
case 0:
    $result = ($height * 3.5) - 108;
    break;
case 1:
    $result = ($height * 4) - 128;
    break;
default:
    $result = 0; 
} echo "Ideal Weight: ". $result .'Unit';
}
?>
<html>
<div align="center">
<body>
<form name="form" method="post" action="<?php echo $PHP_SELF;?>">
    Select Your Gender: <select name="gender">
        <option value=""></option>
        <option value="1">Male</option>
        <option value="0">Female</option>
    </select>
    <br><br>
    Enter Your Height: <input type="number" name="height" placeholder="unit inches">
    <br><br>
    <input type="submit" name="submit" value="Calculate Your Ideal Weight"/>
</form>
</body>
</div>
</html>

1 个答案:

答案 0 :(得分:2)

案例值与选项值不同,并且在关闭开关后回显。尝试:

<?php
if(isset($_POST['submit'])) {

    $gender = isset($_POST['gender']) ? $_POST['gender']: 0;
    $height = (int)$_POST['height'];

    switch ($gender) {
        case 0:
            $result = ($height * 3.5) - 108;
            break;
        case 1:
            $result = ($height * 4) - 128;
            break;
        default:
            $result = 0; 
    }

    echo "Ideal Weight: ". $result .'Unit';

}
?>
<html>
<div align="center">
<body>
    <form name="form" method="post" action="<?php echo $PHP_SELF;?>">
        Select Your Gender: <select name="gender">
            <option value=""></option>
            <option value="1">Male</option>
            <option value="0">Female</option>
        </select>
        <br><br>
        Enter Your Height: <input type="number" name="height" placeholder="unit inches">
        <br><br>
        <input type="submit" name="submit" value="Calculate Your Ideal Weight"/>
    </form>
</body>
</div>
</html>