I am building a data-input form using ajax & jquery for few tables.
The insert query is working fine for all the tables. But for one table where I need to add data into one field as a subtraction of two values coming through form input is not working.
Probably I am making some mistake how to catch the values in js, manipulate them, and call php insert script. I tried a lot bot no success.
Here is input form code. The two fields 'Point-Height
' AND 'Adjustment Value
' are to get values to get Installation-Height = Point-Height - Adjustment Value
and add Installation-Height
in the database
<div>
<div class="col-md-4 mb-3">
<label for="validationCustom02">Point Height</label>
<input type="number" step="0.01" class="form-control" name="point_height" id="point_height" placeholder="insert Point Height" value="0.00" required>
<div class="valid-feedback">
0.00
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustom02">Adjustment Value</label>
<input type="number" step="0.01" class="form-control" name="logger_height" id="logger_height" placeholder="insert Logger Height" value="0.00" required>
<div class="valid-feedback">
0.00
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustom02">Logger Length</label>
<input type="number" step="0.01" class="form-control" name="device_dimension" id="device_dimension" placeholder="insert Device Dimension" value="0.00" required>
<div class="valid-feedback">
0.00
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustom02">Notes</label>
<input type="text" class="form-control" name="notes" id="notes" placeholder="insert notes" value="" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
</div>
<button class="btn btn-primary btn-sm" name="submit6" type="submit" onclick="insertMeasuringDevices()">Insert Records</button>
Here is js for reading values from form, subtract (point height-logger_height), and call php to insert values into database.
function insertMeasuringDevices() {
// get values
var point_height = $("#point_height").val();
var logger_height = $("#logger_height").val();
var notes = $("#notes").val();
var installation_height = point_height-logger_height; //subtracting
var device_dimension = $("#device_dimension").val();
// Add record
$.post("ajax/insertMeasuringDevices.php", {
notes: notes,
installation_height: installation_height,
device_dimension: device_dimension,
}, function (data, status) {
// close the popup
// $("#add_new_record_modal").modal("hide");
// read records again
// readRecords();
/*
// clear fields from the popup
$("#first_name").val("");
$("#last_name").val("");
$("#email").val("");
*/
});
}
// READ records after insert
function readSensors() {
$.get("ajax/readMeasuringDevices.php", {}, function (data, status) {
$(".MeasuringDevices_content").html(data);
});
}
Here is php insertt form to receive data from js to insert values into db.
<?php
if(isset($_POST["submit6"])){
include("ajax/connection.php");
try {
$sql = "INSERT INTO measuring_devices (notes, installation_height, logger_length)
VALUES ('".$_POST["notes"]."','".$_POST["installation_height"]."','".$_POST["device_dimension"]."')";
//echo "<meta http-equiv='refresh' content='0'>";
if ($conn->query($sql)) {
echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
}
else{
echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
exit('<b>Catched exception at line '. $e->getLine() .' (code : '. $e->getCode() .') :</b> '. $e->getMessage());
}
}
//$conn = null;
?>