我对网页设计非常陌生,我正在努力在我的数据库中存储一个 javascript 变量。
我有一个简单的页面,其中包含一个 HTML 表单,其中包含身高/体重/年龄/性别的文本框和一个提交按钮,该按钮调用一个 javascript 函数,该函数执行一些计算以确定用户是体重不足还是超重,然后返回他们应该多少卡路里消耗一天为他们的 BMI 的正确体重。
这是javascript:
<script type="text/javascript">
function bmiCalc()
{
var height=document.getElementById('height box').value,
weight=document.getElementById('weight box').value;
weight/=2.2;
height/=39.37;
BMI=Math.round(weight/(height*height));
alert("Your BMI is :"+BMI);
}
function calorieIntake()
{
var height=document.getElementById('height box').value,
weight=document.getElementById('weight box').value,
age=document.getElementById('age box').value,
gender=document.getElementById('gender box').value;
if(gender=="male" || gender=="Male")
{
if(23<BMI && BMI<25)
{
calories=Math.round((66+(6.23*weight)+(12.7*height)-(6*age)));
alert("You are considered normal weight and should therefore consume: " +calories+" calories a day. This calorie intake will ensure that you remain the same weight");
}
else if(BMI<23)
{
calories=Math.round((66+(6.23*weight)+(12.7*height)-(6*age)+500));
alert("You are considered under-weight, and should therefore consume: " +calories+" calories a day. This calorie intake will cause you to gain one pound a week");
}
else if(BMI>25)
{
calories=Math.round((66+(6.23*weight)+(12.7*height)-(6*age)-500));
alert("You are considered over-weight and should therefore consume: " +calories+" calories a day. This calorie intake will cause you to lose one pound a week");
}
}
else if(gender=="female" || gender=="Female")
{
if(18.49<BMI && BMI<24.9)
{
calories=Math.round(655+(4.35*weight)+(4.7*height)-(4.7*age));
alert("You are considered normal weight and should therefore consume: "+calories+" calories a day. This calorie intake will ensure that you remain the same weight");
}
else if(BMI<18.49)
{
calories=Math.round((655+(4.35*weight)+(4.7*height)-(4.7*age)+500));
alert("You are considered under-weight, and should therefore consume: " +calories+" calories a day. This calorie intake will cause you to gain one pound a week");
}
else if(BMI>24.9)
{
calories=Math.round((655+(4.35*weight)+(4.7*height)-(4.7*age)-500));
alert("You are considered over-weight and should therefore consume: " +calories+" calories a day. This calorie intake will cause you to lose one pound a week");
}
}
}
</script>
这是我的表格:
<form>
<p>Enter your height(in inches):</p><input type="text" id="height box">
<p>Enter your weight(in pounds):</p><input type="text" id="weight box">
<p>Enter your age(in years):</p><input type="text" id="age box">
<p>Enter your gender(male/female):</p><input type="text" id="gender box">
<input type="button" value="Your BMI:" id="button1" onClick="bmiCalc()">
</input>
<input type="button" value="Suggested Calorie Intake:" id="button2" onClick="calorieIntake()">
</input>
</form>
我想要做的是获取“卡路里”变量并将其存储在我的 MySQL 数据库中。我知道我需要以某种方式将 javascript 变量传递给 php 变量,但我不知道该怎么做。这样做的最简单方法是什么?