在提交时使用javascript将值插入数据库

时间:2018-06-21 05:42:58

标签: javascript php

我已经在bmi中创建了表单,我想通过“提交”按钮将bmi的值插入数据库中 但无法识别错误,代码中有什么错误, 值未上载到数据库 我创建了两个文件php和html 获取空值

请任何人指导我!

<html>

<head>
</head>

<body>
    <script>
        function bmi() {

            var height = Number(document.getElementById("height").value);

            var weight = Number(document.getElementById("weight").value);

            var result = (weight / (height * height));
            document.getElementById("result").innerHTML = "Your bmi score is : " + result.toFixed(2);
        }
        if (weight == "") {
            alert("OOPS! PLEASE ENTER weight");
            return true;
        }
    </script>
    <form method="post" action="bmi.php">
        WEIGHT :</font>
        <input id="weight" name="WEIGHT" type="text" placeholder="Your weight in kilograms" />
        <br />
        <br /> HEIGHT :</font>
        <input id="height" name="HEIGHT" type="text" placeholder="Your height in meters" />
        <br />
        <br />
        <input class="submit" type="button" name="BMI" value="BMI" onclick="bmi()" />
        <button type="reset" value="reset">Reset</button>
        <p id="result">
            <font color="yellow">Here will be your result</font>
        </p>
        <p id="bmr">
            <font color="yellow">Here will be your result</font>
        </p>
    </form>
</body>

</html>

MY Php文件代码

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "anuj_db";

// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
if(isset($_POST['BMR'])){
       $sql ="INSERT INTO calculator(GENDER,WEIGHT,AGE,HEIGHT)
       VALUES ('".$_POST["GENDER"]."','".$_POST["WEIGHT"]."','".$_POST["AGE"]."','".$_POST["HEIGHT"]."')";


} 

$conn->close();
?>

2 个答案:

答案 0 :(得分:1)

您的代码是这个

<input class="submit" type="button" name="BMI" value="BMI" onclick="bmi()" />

您正在检查

if(isset($_POST['BMR'])){
   $sql ="INSERT INTO calculator(GENDER,WEIGHT,AGE,HEIGHT)
   VALUES ('".$_POST["GENDER"]."','".$_POST["WEIGHT"]."','".$_POST["AGE"]."','".$_POST["HEIGHT"]."')";
} 

更新代码

if(isset($_POST['BMI'])){
   $sql ="INSERT INTO calculator(GENDER,WEIGHT,AGE,HEIGHT)
   VALUES ('".$_POST["GENDER"]."','".$_POST["WEIGHT"]."','".$_POST["AGE"]."','".$_POST["HEIGHT"]."')";
} 

答案 1 :(得分:0)

<html>
<head>
</head><body>
    <script>
        function bmi () {

        var height = Number(document.getElementById("height").value);

        var weight = Number(document.getElementById("weight").value);

        var result = (weight / (height * height));
        document.getElementById("result").innerHTML = "Your bmi score is : " + result.toFixed(2);

            //Sending data to server (PHP page or .Net Page)
            var xhttp = new XMLHttpRequest();
              xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                  document.getElementById("demo").innerHTML = this.responseText;
                }
              };
            //Your web page where data send by post method
            var url = 'https://www.f5buddy.com/bmi.php';
            //This is sample url

            xhttp.open("POST", url, true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhttp.send('width=width&height=height');
        }
        if (weight ==""){
        alert("OOPS! PLEASE ENTER weight");
        return true;
        }
    </script> 
    <form method="post" action="bmi.php">
          WEIGHT :</font>  <input id="weight" name="weight" type="text" placeholder="Your weight in kilograms" />

               <br />
                <br />

             HEIGHT :</font> <input id="height" name="height" type="text" placeholder="Your height in meters" />

               <br />
                <br />


                <input class = "submit" type="button" name="BMI"  value="BMI" onclick="bmi()" />
                         <button type="reset" value="reset">Reset</button>

               <p id="result"><font color="yellow">Here will be your result</font></p>


               <p id="bmr"><font color="yellow">Here will be your result</font></p>

    </form>  


您的网页,其中数据是通过邮寄方式发送的

var url = 'https://www.f5buddy.com/bmi.php'; 您必须根据自己的喜好更改该网址