简体   繁体   English

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

[英]Store javascript variable in mysql database

I'm very new to web design and I am struggling with storing a javascript variable in my database. 我是Web设计的新手,我正努力将javascript变量存储在数据库中。

I have a simple page with an HTML form that has textboxes for height/weight/age/gender and a submit button that calls a javascript function that does some calculations to determine if the user is under or overweight and then returns how many calories they should consume a day to be the correct weight for their BMI. 我有一个带有HTML表单的简单页面,该表单具有用于身高/体重/年龄/性别的文本框,以及一个提交按钮,该按钮调用javascript函数,该函数进行一些计算以确定用户是否体重不足或超重,然后返回他们应该摄入多少卡路里消耗一天的体重作为其BMI的正确体重。

Here is the javascript: 这是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>

and here is my form: 这是我的表格:

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

What I want to do is take the 'calories' variable and store it in my MySQL database. 我想做的是获取“卡路里”变量并将其存储在我的MySQL数据库中。 I understand I need to somehow pass the javascript variable to a php variable but I don't have any idea how to do that. 我知道我需要以某种方式将javascript变量传递给php变量,但是我不知道该怎么做。 What is the simplest way to go about doing this? 最简单的方法是什么?

There are 2 ways you can go about achieving this. 您可以通过2种方法来实现这一目标。

Option 1 : Use hidden form fields, and set their values through JS before/while submitting the form. 选项1:使用隐藏的表单字段,并在提交表单之前/期间通过JS设置它们的值。 This is easier to achieve, but leaves a bunch of security gaps open. 这比较容易实现,但是仍然存在许多安全漏洞。 This should not be used to send out sensitive data, but may be used if the inputs are sanitized properly via Prepared Statements, or mysqli_real_escape_string . 不应将其用于发送敏感数据,但如果通过Prepared Statements或mysqli_real_escape_string正确地清理了输入,则可以使用mysqli_real_escape_string

Option 2 : Use AJAX to send the data. 选项2:使用AJAX发送数据。 AJAX gives you much more in the way of options and data handling. AJAX为您提供了更多的选项和数据处理方式。 AJAX can be used to send out sensitive data, but you would still need to sanitize on the server side. AJAX可以用于发送敏感数据,但是您仍然需要在服务器端进行清理。

I would look into using AJAX. 我会考虑使用AJAX。 It is not a library or something you have to download, it is just a method of sending and receiving information from JavaScript to the server. 它不是库或您必须下载的东西,它只是从JavaScript向服务器发送和接收信息的一种方法。

You need to initiate an AJAX call to a php page that accepts the value and stores it in your database. 您需要启动一个对php页面的AJAX调用,该页面接受该值并将其存储在数据库中。 In vanilla javascript, the first half of that could look something like this: 在普通javascript中,前半部分可能看起来像这样:

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

Then your PHP page would take the variable ( $_GET['bmi'] ) and pass it to MySQL. 然后,您的PHP页面将使用变量( $_GET['bmi'] )并将其传递给MySQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM