I'm trying to store the users BMI score and comments into mysql database. I'm able to store their height and weight but just not their score and bmi comment. Any suggestions would be appreciated.
function Calculate[]
{
//Obtain user inputs
var height=Number[document.getElementById["height"].value];
var heightunits=document.getElementById["heightunits"].value;
var weight=Number[document.getElementById["weight"].value];
var weightunits=document.getElementById["weightunits"].value;
//Convert all units to metric
if [heightunits=="inches"] height/=39.3700787;
if [weightunits=="lb"] weight/=2.20462;
if [heightunits=="cm"] height/=100;
//Perform calculation
var BMI=weight/Math.pow[height,2];
//Display result of calculation
document.getElementById["output"].innerText=Math.round[BMI*100]/100;
var output = Math.round[BMI*100]/100
if [output=18.5 && output=25 && output30]
document.getElementById["comment"].innerText = "Overweight";
// document.getElementById["answer"].value = output;
}
BMI Calculator
Your BMI is:
This means you are:
asked Mar 8, 2017 at 20:01
Mal1234Mal1234
231 silver badge6 bronze badges
2
One thing you could do is change your span
tags to input
tags and then just change your
JavaScript to set the value
attribute instead of the innerText
. Then when you submit the form, it will pass those values to your PHP script.
You can always style your input boxes to match your paragraph text if don't like the appearance of boxes. For example, start with style='border:none
.
JavaScript
//Display result of calculation
document.getElementById["output"].value=Math.round[BMI*100]/100;
var output = Math.round[BMI*100]/100
if [output=18.5 && output=25 && output30]
document.getElementById["comment"].value = "Overweight";
// document.getElementById["answer"].value = output;
HTML
Your BMI is:
This means you are:
answered Mar 8, 2017 at 20:31
clarmondclarmond
3491 silver badge7 bronze badges
0
Elements with id output and comment are not input
elements but span
, so they are not passed to the PHP
script. You should try to pass this via AJAX to your script, then you can add these values to the request. Example:
var req = new XMLHttpRequest[];
req.onreadystatechange = function[] {
if [this.readyState == 4 && this.status == 200] {
// Whatever you want to do with the php response
}
}
req.open["POST", "script.php", true];
req.setRequestHeader["Content-type", "application/x-www-form-urlencoded"];
req.send["output=" + output + "&weight=" + weight + ...];
Another workaround is to use hidden input
elements, but then you should make a function to validate these or make it non-editable.
answered Mar 8, 2017 at 20:22
C. MirandaC. Miranda
751 silver badge6 bronze badges
Here is a solution to fit your question..
//Display result of calculation
var bmi_result = Math.round[BMI*100]/100;
document.getElementById["output"].innerText=bmi_result;
document.getElementById["form_output"].innerText=bmi_result;
var comment = '';
if [bmi_result=18.5 && output=25 && output30] {
comment = "Overweight";
}
document.getElementById["comment"].innerText = comment;
document.getElementById["form_comment"].innerText = comment;
In your form:
answered Mar 8, 2017 at 20:36
YoloYolo
1,5491 gold badge11 silver badges15 bronze badges