• 0

JS vars not passing to php?


Question

Hi, I'm baffled here; the two following statements from my JS
file displays the correct values but the code to enter them
into a table enters zeroes. I tried document.write(s) and the
correct values were displayed.what am I missing? Thanks in advance.

var expression = value1 + op +value2 +'='+ total;alert(expression);
<?phpinclude('calcbegin.html');$servername = 
"localhost";$username = "root";table$password = "cookie";$dbname = 
"homedb"; show// Create connection$conn = new 
mysqli($servername, $username, $password, $dbname); // Check 
connection if ($conn->connect_error) { die("Connection 
failed: " . $conn->connect_error); }
$sql = "INSERT INTO calculator (purpose, value1, op, value2, total)VALUES 
('$purpose', '$value1', '$op', '$value2', '$total')";if 
($conn->query($sql) === TRUE) { echo "New record created 
successfully"; } else { echo "Error: " . $sql . "<br>" . 
$conn->error; }$conn->close(); ?>
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Your HTML/Javascript page/app needs to submit/make a request to the PHP script that will be inserting the records into the database.

 

It seems you're including the HTML file in your PHP script and are expecting some Javascript variables to magically be accessable to your PHP script, it doesn't work like that.

 

You can either use an HTML form or AJAX to submit the data to the PHP script.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.