我正在尝试将用户的 BMI 分数和评论存储到 mysql 数据库中。我可以存储他们的高度和体重,但不能存储他们的分数和体重指数评论。 任何建议,将不胜感激。
<script type="text/javascript">
function Calculate()
{
//Obtain user inputs
var height=Number(document.getElementById("height").value);
var heightunits=document.getElementById("heightunits").value;
var weight=Number(document.getElementById("weight").value);
var weightunits=document.getElementById("weightunits").value;
//Convert all units to metric
if (heightunits=="inches") height/=39.3700787;
if (weightunits=="lb") weight/=2.20462;
if (heightunits=="cm") height/=100;
//Perform calculation
var BMI=weight/Math.pow(height,2);
//Display result of calculation
document.getElementById("output").innerText=Math.round(BMI*100)/100;
var output = Math.round(BMI*100)/100
if (output<18.5)
document.getElementById("comment").innerText = "Underweight";
else if (output>=18.5 && output<=25)
document.getElementById("comment").innerText = "Normal";
else if (output>=25 && output<=30)
document.getElementById("comment").innerText = "Obese";
else if (output>30)
document.getElementById("comment").innerText = "Overweight";
// document.getElementById("answer").value = output;
}
</script>
</head>
<body>
<div>BMI Calculator</div>
<input type="button" value="Calculate" onclick="Calculate(this.form);">
<input type="submit" name="savebmi" value="SaveBMI">
<p class="font-3">Your BMI is: <span name="output" value="output" type="float" id="output" class="textInput"></span></p>
<p class="font-3">This means you are: <span name="comment" type="text" id="comment"></span> </p>
最佳答案
您可以做的一件事是将 span
标记更改为 input
标记,然后只需更改 JavaScript 以设置 value
属性,而不是innerText
。然后,当您提交表单时,它会将这些值传递给您的 PHP 脚本。
如果不喜欢输入框的外观,您可以随时设置输入框的样式以匹配段落文本。例如,以 style='border:none
开头。
JavaScript
//Display result of calculation
document.getElementById("output").value=Math.round(BMI*100)/100;
var output = Math.round(BMI*100)/100
if (output<18.5)
document.getElementById("comment").value = "Underweight";
else if (output>=18.5 && output<=25)
document.getElementById("comment").value = "Normal";
else if (output>=25 && output<=30)
document.getElementById("comment").value = "Obese";
else if (output>30)
document.getElementById("comment").value = "Overweight";
// document.getElementById("answer").value = output;
HTML
<p class="font-3">Your BMI is: <input name="output" type="float" id="output" class="textInput" style="border:none" /></p>
<p class="font-3">This means you are: <input name="comment" type="text" id="comment" style="border:none" /></p>
关于javascript - 如何将javascript值存储到mysql数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42680852/