Well, 12Strings, everyone who has posted have explained how to handle this. Perhaps a short recap on
how JS and PHP works might help you.
Javascript is CLIENT-SIDE only. Therefore, it can only alter fields that are held inside the browser. It can
do calculations and store values as needed into fields. Therefore, it does nothing SERVER-SIDE and can’t
directly store data on your server.
Transferring data from the browser to the server can be handled, in the easy ways, by either posting the
data inside input fields using a form and sending the posted values to a PHP file that can handle storing the
values or using AJAX to basically do the same thing using a smaller PHP file that updates the database.
Therefore, you need to to either POST the form to a PHP file that would grab the values using $_POST[]
variables and store the data OR you need to call a PHP file from your JS code which would mean using the
AJAX code. Both of these are simple methods and several posts here have shown you how to do that.
In your code, you have this line:
$sql = "INSERT INTO calculator (purpose, value1, op, value2, total)
VALUES ('purpose', 'value1', 'op', 'value2', 'total')";
You attempt to store data into table "calculator" using data that is invalid. In PHP, you need to use variables
which are normally in the form of $purpose, $value1, $op, $value2, $total, etc... Also, these variables would
need loading from the page that is posting the data. Therefore, if using a FORM and POST system, you just
capture them something like: $purpose=$_POST["purpose"]; Where the name of the input field on the
form page is named "purpose"... If you use the AJAX method, you create a list of the data that is sent to
the PHP file. Then, the PHP file would use AJAX and JSON data to handle the data and then load the list of
variables that are needed for the SQL command. Hope that makes sense...
So, your first section is a form which is set up to use POST, therefore, I think the first way would work best
and might be easier to understand than the AJAX version. In your code sample, you set up the EQUALS
button and send it to a JS function. I assume that this function handles the calculation code. In the second
section, you show a CALCULATE button that submits the form. This buttons sends the form data to the
file “calcinsert.php”. Since you did not explain which samples of your code are in which filenames, I will
assume that the “calcinsert.php” file is the last section where you store the info into the DB. If that is the
case, then, in that section you would need to add in the code that grabs the values from the form before it
is stored in the DB. If all of this is true, you would need to add some code something like this. (this is just
off the top of my head and not tested at all.)
[php]
<?php
$servername = "localhost";$username = "root";$password = "xxxxxx";$dbname = "homedb";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{ die("Connection failed: " . mysqli_connect_error()); }
// Added lines to grab the form's values...
$purpose = $_POST['purpose'];
$value1 = $_POST['value1'];
$op = $_POST['op'];
$value2 = $_POST['value2'];
$total = $_POST['total'];
// Altered the INSERT to use the values pulled from the form...
$sql = "INSERT INTO calculator (purpose, value1, op, value2, total)
VALUES ('$purpose', '$value1', '$op', '$value2', '$total')";
// ( NOTE: notice that names in the DB do not use the dollar-sign, but, variables do... )
if (mysqli_query($conn, $sql))
{ echo "New record created successfully"; }
else
{ echo "Error: " . $sql . "
" . mysqli_error($conn); }
mysqli_close($conn);
header( 'Location: http://localhost/home/calculator.html' );
exit();
?>
[/php]
( I x’d out your password as you should not post that on forums. )
As you see, the form needs to have the input fields “named” correctly for the PHP code to grab the values
of them and then they are useable as PHP variables. So, this is the first way of posting data to a PHP file
using a standard form system. One further issue is that in your form code will start out blank once the PHP
code sends itself back to the HTML page. Therefore, in that page, you will have to re-insert the values into
the form’s input fields. Unless you want it to clear after each save. (That is why others would like to use
the AJAX method as it can be done dynamically. But, that means adding in the AJAX code. Either works!)
Hope that all makes sense and helps…