I am creating a web page in HTML which contains javascript and a small form with a submit button which calls the function of the whole javascript code.
This should work as a BMR + Maintenance Calorie Calculator. Using formulas shown on:
- http://www.bmi-calculator.net/bmr-calculator/bmr-formula.php
- http://www.bmi-calculator.net/bmr-calculator/harris-benedict-equation/
Male Metric BMR=66+(13.7 x weight in kilos)+(5 x height in cm)-(6.8 x age in years)
Female Metric BMR=655+(9.6 x weight in kilos)+(1.8 x height in cm)-(4.7 x age in years)
To then discovers ones Daily Maintenance Calorie Requirements (to stay the same weight), there are multipliers depending on activity level. It was when creating these IF statements that I started having these problems.
The issue: When I click submit, nothing happens. It used to send the user to a new page where it displayed the document.write text, however now, it does nothing.
I assume this is a fairly simple issue to solve but i'm new to javascript so any help would be appreciated, many thanks!
<html>
<head>
<title>BMR Calculator</title>
</head>
<body>
<h1> BMR Calculator and Daily Maintenance Calorie Calculator </h1>
<script type="text/javascript" language="javascript">
function Calculate() {
var gender = document.getElementById("gender").value;
var weight = document.getElementById("weight").value;
var height = document.getElementById("height").value;
var age = document.getElementById("age").value;
var result = document.getElementById("result").value;
var MaintenanceCalories = document.getElementById("MaintenanceCalories").value;
var activitylevel = document.getElementById("activitylevel").value;
if(gender=="male")
//English-BMR = 66 + ( 6.23 x weight in pounds ) + ( 12.7 x height in inches ) - ( 6.8 x age in year )
//Metric-BMR = 66 + ( 13.7 x weight in kilos ) + ( 5 x height in cm ) - ( 6.8 x age in years )
{
val1 = 13.7 * weight;
val2 = 5 * height;
val3 = 6.8 * age;
result = 66 + val1 + val2 - val3;
val4 = activitylevel;
}
else if (gender=="female")
//English-Women: BMR = 655 + ( 4.35 x weight in pounds ) + ( 4.7 x height in inches ) - ( 4.7 x age in years)
//Women: BMR = 655 + ( 9.6 x weight in kilos ) + ( 1.8 x height in cm ) - ( 4.7 x age in years )
{
val1 = 9.6 * weight;
val2 = 1.8 * height;
val3 = 4.7 * age;
result = 655 + val1 + val2 - val3;
val4 = activitylevel;
}
if(val4=="l")
{
MaintenanceCalories = result * 1.2;
}
if(val4=="lm")
{
MaintenanceCalories = result * 1.375;
}
if(val4=="m")
{
MaintenanceCalories = result * 1.55;
}
if(val4=="mh")
{
MaintenanceCalories = result * 1.725;
}
else if(val4=="h")
{
MaintenanceCalories = result * 1.9;
}
document.write ('Your BMR is: ' + result.toFixed(2) + '. Your Maintenance Calories are: ' + MaintenanceCalories.toFixed(2));
}
</script>
<form action="#">
Gender : <select id="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select><br/>
Weight (kg.) : <input type="text" id="weight" /><br />
Height (cm): <input type="text" id="height" /><br />
Age : <input type="text" id="age" /><br />
Current Activity Level: <select id="activitylevel">
<option value="l">Sedentary (Little or No Exercise)</option>
<option value="lm">Lightly Active (Light Exercise/Sports 1-3 Days Per Week)</option>
<option value="m">Moderately Active (Moderate Exercise/Sports 3-5 Days Per Week)</option>
<option value="mh">Very Active (Hard Exercise/Sports 6-7 days Per Week)</option>
<option value="h">Extra Active (Very Intense Exercise/Sports and Physical Job Daily)</option>
</select>
</br>
</br>
</fieldset>
<input type="button" value="Submit" onclick="Calculate()"/>
</form>
It used to send the user to a new page- not withdocument.writeit didn't