0

我对网页设计非常陌生,我正在努力在我的数据库中存储一个 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 变量,但我不知道该怎么做。这样做的最简单方法是什么?

4

3 回答 3

2

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

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

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

于 2013-07-31T23:44:20.570 回答
0

我会考虑使用 AJAX。它不是一个库或你必须下载的东西,它只是一种从 JavaScript 向服务器发送和接收信息的方法。

于 2013-07-31T23:44:02.133 回答
0

您需要对接受该值并将其存储在数据库中的 php 页面发起 AJAX 调用。在 vanilla javascript 中,前半部分可能如下所示:

var xhr = new XMLHttpRequest();
xhr.open("get","/path/to/php/page.php?bmi=calculated_bmi");
xhr.send();

然后您的 PHP 页面将获取变量 ( $_GET['bmi']) 并将其传递给 MySQL。

于 2013-07-31T23:44:13.017 回答