Note that there are some explanatory texts on larger screens.

plurals
  1. POTransfer javascript variable to php using hidden forms?
    text
    copied!<p>I have a form on my page that asks users to enter in their height/weight and it calculates their BMI and stores it in a database. I am hung up on how I transfer the javascript variable to a php variable. I understand I have to use hidden forms but I can't seem to figure out how they work. </p> <p>here is my code</p> <pre><code>&lt;?php include "base.php"; ?&gt; &lt;?php session_start (); ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt;BMI&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="bmi" action="main.php" method="post"&gt; &lt;p&gt;Enter your height(in inches):&lt;/p&gt;&lt;input type="text" id="height box" name="height box"&gt; &lt;p&gt;Enter your weight(in pounds):&lt;/p&gt;&lt;input type="text" id="weight box" name="weight box"&gt; &lt;input type="hidden" id="bmi" name="bmi"&gt; &lt;input type="button" value="Calculate" id="calc button" name="calc button" onClick="calc()"&gt; &lt;/input&gt; &lt;/form&gt; &lt;script type="text/javascript"&gt; function calc() { // get variables from textboxes var height=document.getElementById('height box').value; var weight=document.getElementById('weight box').value; var bmi=document.getElementById('bmi').value; // calculate BMI weight/=2.2; height/=39.37; BMI=Math.round(weight/(height*height)); &lt;/script&gt; &lt;?php //insert username, date, and bmi into the db $username = mysql_real_escape_string($_SESSION['Username']); $date = date('Y-m-d'); $bmi = mysql_real_escape_string($_POST['bmi']); mysql_query("INSERT INTO bmi (username, bmi, date) VALUES('".$username."', '".$bmi."', '".$date."')"); ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>base.php is just where I do my connect to the sql server and select the database stuff</p> <p>The javascript bmi variable does not seem to be transferring to the php $bmi variable. What is it I am doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload