PHP不会预先进行calucation

时间:2015-05-29 17:13:00

标签: php

我有一个php表单,用户输入他们的身高和体重,一旦他们点击提交,信息将重新提交到表单中,并且bmi的calucation应该执行并显示结果。我有点不确定如何使用我的代码执行此操作并且我已经检查了错误phpcodechecker.com找不到,但我没有得到结果。所以我不确定计算是否真的发生了。任何关于如何计算用户输入信息的BMI的帮助都将不胜感激。

我需要使用的是

BMI =(以磅为单位的重量* 703)/(以英寸为单位的高度)的平方 注意:有12英寸到一英尺 因此,如果一个人体重150磅并且身高6英尺,那么计算就是 BMI =(150 * 703)/ 72 x 72(6英尺= 72英寸,然后是平方)

以下代码:         

  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="weight">Enter your weight in pounds:</label><br>
    <input id="weight" name="weight" type="text" value="<?php echo $weight; ?>" size="30"><br>
    <label for="height">Enter your height in inches:</label><br>
    <input id="height" name="height" type="text" value="<?php echo $height; ?>" size="30"><br>
    <input type="submit" name="submit" value="Calculate">
  </form>

<?php
  }
?>
<?php
$weight = '';
$height = '';

  if (isset($_POST['submit'])) {
    $weight = $_POST['weight'];
    $height = $_POST['height'];
    $output_form = false;

    if (empty($weight) && empty($height)) {
      echo 'You forgot to enter your weight.<br />';
      $output_form = true;
    }

    if (empty($weight) && (!empty($height))) {
      echo 'You forgot to enter your height.<br />';
      $output_form = true;
    }
  }
  else {
    $output_form = true;
  }

  if ((!empty($weight)) && (!empty($height))) {
      $string = "(weight * 703)/height * height";
      eval($string);
      echo($res);
?>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="weight">Enter your weight in pounds:</label><br>
    <input id="weight" name="weight" type="text" value="<?php echo $weight; ?>" size="30"><br>
    <label for="height">Enter your height in inches:</label><br>
    <input id="height" name="height" type="text" value="<?php echo $height; ?>" size="30"><br>
    <input type="submit" name="submit" value="Calculate">
  </form>

<?php
  }
?>

3 个答案:

答案 0 :(得分:2)

这看起来不对:

  $string = "(weight * 703)/height * height";
  eval($string);
  echo($res);
  1. 没有$res变量;
  2. 无需评估用户输入。这实际上是一个非常糟糕的主意,因为它带来了巨大的安全风险。
  3. 你应该只计算你需要的值并将其反映出来:

     echo ($weight * 703)/$height * $height;
    

    除此之外你没有使用$output_form变量,但我猜你还没有添加它。

答案 1 :(得分:2)

一些提示:

  • 避免在代码中使用eval。这是缓慢而不安全的。
  • 看起来你有两次相同的表格(重复的代码)。另外,尽量避免这种情况。我改变了你的代码,所以你不必重复它

更好的工作代码可能如下所示:

<?php
$weight = '';
$height = '';
$output_form = true;

if (isset($_POST['submit'])) {

    $weight = $_POST['weight'];
    $height = $_POST['height'];

    if (empty($weight) && empty($height)) { //test for both
        echo 'You forgot to enter your weight and height.<br>';
    }else if (empty($weight)) {
        echo 'You forgot to enter your weight.<br />';
    }else if (empty($height)) {
        echo 'You forgot to enter your height.<br />';
    }else{ //if none is empty, you can proceed with calc
        $output_form = false; //everything ok, hide the form
        $res = ($weight * 703)/($height * $height);
        echo $res;
    }
}

if($output_form) { ?>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <label for="weight">Enter your weight in pounds:</label><br>
        <input id="weight" name="weight" type="text" value="<?php echo $weight; ?>" size="30"><br>
        <label for="height">Enter your height in inches:</label><br>
        <input id="height" name="height" type="text" value="<?php echo $height; ?>" size="30"><br>
        <input type="submit" name="submit" value="Calculate">
    </form>

<?php }

答案 2 :(得分:0)

您走在正确的轨道上,只需执行以下操作:

if ((!empty($weight)) && (!empty($height))) {
  $result = ($weight * 703) / $height * $height;
  echo $result;
  // ...
相关问题