dongru2019 2013-07-31 23:35
浏览 53
已采纳

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

I'm very new to web design and I am struggling with storing a javascript variable in my database.

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.

Here is the 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. 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. What is the simplest way to go about doing this?

  • 写回答

3条回答 默认 最新

  • dongyuanguang3893 2013-07-31 23:44
    关注

    There are 2 ways you can go about achieving this.

    Option 1 : Use hidden form fields, and set their values through JS before/while submitting the form. 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.

    Option 2 : Use AJAX to send the data. AJAX gives you much more in the way of options and data handling. AJAX can be used to send out sensitive data, but you would still need to sanitize on the server side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)