首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将javascript值存储到mysql数据库中?

如何将javascript值存储到mysql数据库中?
EN

Stack Overflow用户
提问于 2017-03-08 20:01:38
回答 3查看 4K关注 0票数 0

我试图将用户的BMI评分和评论存储到mysql数据库中。我可以存储他们的身高和体重,但不包括他们的分数和bmi的评论。如有任何建议,将不胜感激。

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-03-08 20:31:09

您可以做的一件事是将span标记更改为input标记,然后更改JavaScript以设置value属性而不是innerText。然后,当您提交表单时,它将把这些值传递给PHP脚本。

如果不喜欢框的外观,则可以将输入框的样式设置为与段落文本匹配。例如,从style='border:none开始。

JavaScript

代码语言: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

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2017-03-08 20:22:35

具有id输出和注释的元素不是input元素,而是span元素,因此它们不会传递给PHP。您应该尝试通过AJAX将其传递给您的脚本,然后可以将这些值添加到请求中。示例:

代码语言:javascript
复制
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        // Whatever you want to do with the php response
    }
}
req.open("POST", "script.php", true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send("output=" + output + "&weight=" + weight + ...);

另一个解决方法是使用隐藏的input元素,但是您应该创建一个函数来验证这些元素,或者使其不可编辑。

票数 0
EN

Stack Overflow用户

发布于 2017-03-08 20:36:19

这是一个适合你的问题的解决方案。

代码语言:javascript
复制
//Display result of calculation
var bmi_result = Math.round(BMI*100)/100;

document.getElementById("output").innerText=bmi_result;
document.getElementById("form_output").innerText=bmi_result;

var comment = '';
if (bmi_result<18.5) {
   comment  = "Underweight";
} else   if (bmi_result>=18.5 && output<=25) {
   comment  = "Normal";
} else   if (bmi_result>=25 && output<=30) {
   comment  = "Obese";
} else   if (bmi_result>30) {
   comment  = "Overweight";
}

document.getElementById("comment").innerText = comment;
document.getElementById("form_comment").innerText = comment;

以你的形式:

代码语言:javascript
复制
<input type="hidden" id="form_output" name="output" value="">
<input type="hidden" id="form_comment" name="comment" value="">
<input type="button" value="Calculate" onclick="Calculate(this.form);">
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42680852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档