0

I'm creating a simple BMR calculator that calculates the following formula: BMR = 66 + ( 6.23 x weight in pounds ) + ( 12.7 x height in inches ) - ( 6.8 x age in year ). This code has been borrowed and modified so I'm not completely comfortable with it.

Im curious to as why the '66' is already displayed in answer before I press submit. How can I change my code to not display any values in the answer box until submit is pressed?

The calculator can be found here: http://rgoo.co/calculators/bmr-calculator.php

<?php
if (isset($_POST['agev'])) $agev = $_POST['agev'];
if (isset($_POST['feetv'])) $feetv = $_POST['feetv'];
if (isset($_POST['inchesv'])) $inchesv = $_POST['inchesv'];
if (isset($_POST['weightv'])) $weightv = $_POST['weightv'];

$totalheightv = $inchesv + ($feetv*12);
$answer = 66 + (6.23*$weightv) + (12.7*$totalheightv) - (6.8*$agev);
echo <<<_END
<form method='post' action='/calculators/bmr-calculator.php'>
<table border='0' width='500px' cellpadding='2' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>IIFYM test</strong></td></tr>
<tr class="calcrow"><td>AGE:</td><td align="center"><input type='text' name='agev' value="$agev"/>Years</td></tr>
<tr class="calcrow2"><td>Height:</td><td align="center"><input type='text' name='feetv' value="$feetv"/>Ft</td></tr><td align="center"><input type='text' name='inchesv' value="$inchesv"/>In</td></tr>
<tr class="calcrow"><td>Weight:</td><td align="center"><input type='text' name='valuec' value="$valuec"/>lbs</td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>

<tr class="calcrow">
<td>Your BMR is:</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td>
</tr>
</table>
</form>

3 Answers 3

3

Default to an empty value for your answer and only calculate when you have all your values:

<!doctype html>
<?php
$answer = "";
$agev = "";
$feetv = "";
$inchesv = "";
$weightv = "";

if (isset($_POST['agev']) && isset($_POST['feetv']) && isset($_POST['inchesv']) && isset($_POST['weightv'])) {
    $agev = $_POST['agev'];
    $feetv = $_POST['feetv'];
    $inchesv = $_POST['inchesv'];
    $weightv = $_POST['weightv'];

    $totalheightv = $inchesv + ($feetv*12);
    $answer = 66 + (6.23*$weightv) + (12.7*$totalheightv) - (6.8*$agev);

}

echo <<<_END
<form method='post' action=''>
<table border='0' width='500px' cellpadding='2' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>IIFYM test</strong></td></tr>
<tr class="calcrow"><td>AGE:</td><td align="center"><input type='text' name='agev' value="$agev"/>Years</td></tr>
<tr class="calcrow2"><td>Height:</td><td align="center"><input type='text' name='feetv' value="$feetv"/>Ft</td></tr><td align="center"><input type='text' name='inchesv' value="$inchesv"/>In</td></tr>
<tr class="calcrow"><td>Weight:</td><td align="center"><input type='text' name='weightv' value="$weightv"/>lbs</td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>

<tr class="calcrow">
<td>Your BMR is:</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td>
</tr>
</table>
</form>

This way you ensure that

  • the answer field is empty if nothing was submitted
  • all required values are available to ensure that the BMR is calculated properly
  • you get no PHP warnings for using undefined variables

And I fixed a small issue you had in your version of the code, where you used the name valuec for the weight input field as opposed to weightv in your code. And please add any missing HTML tags (like the doctype, <head>, <body>, etc.).

3
  • I currently have your suggested code uploaded to rgoo.co/calculators/bmr-calculator.php The 66 is cleared to 0 as expected but When values are entered and submit is pressed, the answer does not output
    – ryank
    Dec 27, 2013 at 9:39
  • @RyanGoo Obviously not, because your code example is wrong. You use a different variable name for the weight: weightv VS valuec. I fixed this for you in my answer.
    – nietonfir
    Dec 27, 2013 at 9:50
  • Ahh Thank you! I appreciate the explanation for your solutions as well
    – ryank
    Dec 27, 2013 at 10:00
1

Do something like

$answer = '';
if(isset($_POST["usubmit"]) && $_POST["usubmit"] != '')
{
  $answer = 66 + (6.23*$weightv) + (12.7*$totalheightv) - (6.8*$agev);
}

Then in the html form add a name to your submit button as "usubmit"

<tr class="submit">
   <td colspan="2">
        <input type='submit' name='usubmit'  value='Calculate'/>
   </td>
</tr>

The number 66 is getting shown since on page load it will be assigned to your variable. You can avoid this by adding the condition so that it calculates only when the submit is done.

0

Quite simply said, before you echo round($answer) you define a mathematical assignment:

$answer = 66 + (6.23*$weightv) + (12.7*$totalheightv) - (6.8*$agev);

Basing the value on 66. So it is already set, when later on in the code you call

echo($answer);
1
  • I understand what you're saying, any suggestions on how to keep the answer field blank until submit is pressed? My knowledge of php functions is minimal.
    – ryank
    Dec 27, 2013 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.