// Get the product name and price from the user
var name = $("#name").val();
var price = $("#price").val();
// Create a JSON object to send to the server
var data = {
name: name,
price: price
};
// Make an AJAX request to the server
$.ajax({
type: "POST",
url: "calculate_price.php",
data: data,
success: function(response) {
// Display the total price
$("#total_price").text(response);
}
});
Code (PHP)
// Connect to MySQL
$conn = mysqli_connect('localhost', 'username', 'password', 'database');
// Get the product name and price from the user
$name = $_POST['name'];
$price = $_POST['price'];
// Get the percent price from the Percenttb table
$sql = "SELECT percentprice FROM Percenttb WHERE name = $name";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$percentprice = $row['percentprice'];
// Calculate the price of the product
$totalprice = $price + ($price * $percentprice) / 100;
// Display the total price
echo $totalprice;
// Close the connection to MySQL
mysqli_close($conn);
<script>
function myGpFunction() {
var price = $("#price").val();
var data = {
name: name,
price: price
};
// Make an AJAX request to the server
$.ajax({
type: "POST",
url: "calgp.php",
data: data,
success: function(response) {
// Display the total price
$("#total_price").text(response);
}
});
}
</script>
<div class="row">
<input name="price" class="form-control" autocomplete="off" placeholder="ราคา" onchange="myGpFunction();" style="width:100%" required>
</div>
<div class="total_price"></div> <!-- ที่ต้องการให้แสดงค่าออกมา -->
ไฟล์ calgp.php Code (PHP)
<?php
require_once "connectdb.php";
// Get the product name and price from the user
$price = $_POST['price'];
// Get the percent price from the Percenttb table
$sqlcal = "SELECT gppercent FROM merchantgptb WHERE id = 1";
$resultcal = mysqli_query($conn, $sqlcal);
$rowcal = mysqli_fetch_assoc($resultcal);
$gppercent = $rowcal['gppercent'];
// Calculate the price of the product
$totalprice = $price + ($price * $gppercent) / 100;
// Display the total price
echo $totalprice;
// Close the connection to MySQL
mysqli_close($conn);
?>