How store javascript value in database?

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.


 
 

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)
   document.getElementById("comment").value = "Underweight";
   else   if (output>=18.5 && output<=25)
   document.getElementById("comment").value = "Normal";
   else   if (output>=25 && output<=30)
   document.getElementById("comment").value = "Obese";
   else   if (output>30)
   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

How store javascript value in database?

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) {
   comment  = "Underweight";
} else   if (bmi_result>=18.5 && output<=25) {
   comment  = "Normal";
} else   if (bmi_result>=25 && output<=30) {
   comment  = "Obese";
} else   if (bmi_result>30) {
   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

Not the answer you're looking for? Browse other questions tagged javascript php mysql or ask your own question.

Can JavaScript store data in database?

JavaScript allows us to store data in the browser using local storage API. Here, you can use LocalStorage and SessionStorage . The objects let us store data (in key/value pairs) and update it from the browser's storage. To view the data, open your browser.

How can we store form data in database using JavaScript?

Use Case: Create a New Order.
Define Queries. We will define two queries. ... .
Generate XML Schema. ... .
Create the Form. ... .
Link to the Database. ... .
Define the SQL Query. ... .
Generate an XML Schema. ... .
Create the Form. ... .
Link to the Database..

How can we store data in JavaScript without database?

You can use web storage. From W3Schools: With web storage, web applications can store data locally within the user's browser. Before HTML5, application data had to be stored in cookies, included in every server request.

How use JavaScript variable in php SQL query?

php $var1 = $_POST['var1']; $var2 = $_POST['var2']; $getvalue="SELECT id,name from table1 WHERE column1='$var1' and column2='$var2'"; $result=mysql_query($getvalue) or die(mysql_error()); while($row=mysql_fetch_array($result)){ extract($row); echo $name; } ?>