Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将javascript值存储到mysql数据库中?_Javascript_Php_Mysql - Fatal编程技术网

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

如何将javascript值存储到mysql数据库中?,javascript,php,mysql,Javascript,Php,Mysql,我正在尝试将用户的BMI分数和评论存储到mysql数据库中。我可以存储他们的身高和体重,但不能存储他们的分数和bmi注释。 如有任何建议,将不胜感激 <script type="text/javascript"> function Calculate() { //Obtain user inputs var height=Number(document.getElementById("height").value); v

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

<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>

函数计算()
{
//获取用户输入
var height=数字(document.getElementById(“height”).value);
var heightunits=document.getElementById(“heightunits”).value;
var-weight=Number(document.getElementById(“weight”).value);
var weightunits=document.getElementById(“weightunits”).value;
//将所有单位转换为公制
如果(高度单位=“英寸”)高度/=39.3700787;
如果(重量单位=“磅”)重量/=2.20462;
如果(高度单位=“cm”)高度/=100;
//计算
var BMI=体重/数学功率(身高,2);
//显示计算结果
document.getElementById(“输出”).innerText=Math.round(BMI*100)/100;
var输出=数学整数(BMI*100)/100
如果(输出=18.5&&output=25&&output30)
document.getElementById(“comment”).innerText=“超重”;
//document.getElementById(“应答”).value=输出;
}
体重指数计算器

您的体重指数是:

这意味着您:

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


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

您可以做的一件事是将
span
标记更改为
input
标记,然后只需更改JavaScript以设置
属性,而不是
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; 
//显示计算结果
document.getElementById(“输出”).value=Math.round(BMI*100)/100;
var输出=数学整数(BMI*100)/100
如果(输出=18.5&&output=25&&output30)
document.getElementById(“comment”).value=“超重”;
//document.getElementById(“应答”).value=输出;
HTML

您的体重指数是:

这意味着您:


这里有一个适合您的问题的解决方案

//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;
//显示计算结果
var bmi_结果=数学四舍五入(bmi*100)/100;
document.getElementById(“输出”).innerText=bmi\u结果;
document.getElementById(“表单输出”).innerText=bmi\U结果;
var注释=“”;
如果(bmi_结果=18.5&&output=25&&output30){
评论=“超重”;
}
document.getElementById(“comment”).innerText=comment;
document.getElementById(“form_comment”).innerText=comment;
以您的形式:

<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);">


您需要将bmi保存到一个隐藏的表单字段,或者只需在后端重新计算它。谢谢您的回答!你知道我会怎么做吗?
//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;
<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);">