Forums

  • # February 20, 2014 at 1:15 am
    <?php
    // Attila Hajzer
    $weight = $_POST["txtWeight"];
    $height = $_POST["txtHeight"];
    $unit = $_POST["optUnit"];
    $message = "";
    
    if($unit == "metric")
    {
        $bmiCalc = $weight/($height*$height); #This Line
        $message = "Metric BMI: $bmiCalc";
    }
    else
    {
        $bmiCalc = ($weight * 703)/($height * $height);
        $message = "Imperial BMI: $bmiCalc";
    }
    
    ?>
    

    What am I doing wrong ?

    # February 20, 2014 at 1:32 pm

    Your code worked on my local machine with some sample data… I tried replicating the data as if everything was a string and it worked. But I suggest you type juggle and deal with appropriate data types.

    This worked for me as well:

    // $weight = (float) $_POST["txtWeight"];
    // $height = (float) $_POST["txtHeight"];
    // $unit = (string) $_POST["optUnit"];
    // Our Sample Data
    $weight = (float) '185.33';
    $height = (float) '5.11';
    $unit = (string) ' ';
    $message = ' ';
    
    switch ($unit) {
      case 'metric':
       $bmiCalc = $weight/($height*$height);
       echo $message = 'Metric BMI: ' . $bmiCalc;
      break;
    
      default:
        $bmiCalc = ($weight * 703)/($height * $height);
        echo $message = 'Imperial BMI: ' . $bmiCalc;
      break;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag