 |
รบกวนสอบถามเกี่ยวกับการคำนวณค่าใน Form แบบทันที โดยไม่ต้องกด Submit ก่อน |
|
 |
|
|
 |
 |
|
ศึกษา AJAX
Code (JavaScript)
// 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);
|
 |
 |
 |
 |
Date :
2023-06-10 11:58:36 |
By :
009 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คำนวนที่ฝั่ง javascript น่าจะเหมาะกว่าไหมครับ
|
 |
 |
 |
 |
Date :
2023-06-13 09:06:45 |
By :
birdbird27 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|