php - 将javascript变量存储在mysql数据库中

标签 php javascript mysql sql database

我对网页设计非常陌生,并且在数据库中存储 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 变量,但我不知道如何做到这一点。最简单的方法是什么?

最佳答案

有两种方法可以实现这一目标。

选项1:使用隐藏的表单字段,并在提交表单之前/同时通过JS设置它们的值。这更容易实现,但留下了许多安全漏洞。这不应该用于发送敏感数据,但如果通过准备好的语句或mysqli_real_escape_string正确清理输入,则可以使用。

选项 2: 使用 AJAX 发送数据。 AJAX 为您提供了更多选项和数据处理方式。 AJAX 可用于发送敏感数据,但您仍然需要在服务器端进行清理。

关于php - 将javascript变量存储在mysql数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17982810/

相关文章:

php - 如何防止 PHP 中的 SQL 注入(inject)?

javascript - 如果包含php的div标签为空,则隐藏div或显示某些文本

mysql - 如何正确缓存/更新使用远程数据库的数据驱动 iPhone 应用程序?

php - 两个表的下拉列表,php mysql

php - 在 while 循环中执行函数

javascript - Angularjs参数不是一个得到字符串的函数

javascript - 将行添加到 ASP.NET MVC 表时更新连接的数据库表

mysql存储过程比标准查询慢20倍

PHP : Finding Files on Directory

php - 在表单/电子邮件中复制/粘贴后出现奇怪的字符