This commit is contained in:
u1 2024-11-14 14:37:28 +01:00
parent 616a1e2e68
commit 2d1185ff07
4 changed files with 75 additions and 0 deletions

36
js/post.js Normal file
View File

@ -0,0 +1,36 @@
document.getElementById('btn').addEventListener('click', async () => {
const fuelType = document.getElementById('fuel').value;
const liters = document.getElementById('price').value;
// Spakuj dane w formacie JSON
const data = {
nazwa: fuelType,
cena: parseFloat(liters) // konwertuj na liczbę, jeśli wartość jest liczbową
};
console.log("ok");
try {
// Wykonaj żądanie POST
const response = await fetch('php/post.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
// Przetwarzanie odpowiedzi
if (response.ok) {
const result = await response.json();
console.log('Odpowiedź serwera:', result);
alert(`Koszt paliwa: ${result.cost}`);
} else {
console.error('Błąd:', response.statusText);
}
} catch (error) {
console.error('Błąd:', error);
}
});

15
php/get.php Normal file
View File

@ -0,0 +1,15 @@
<?php
$polaczenie = mysqli_connect("172.18.0.3", "root", "secret", "db");
$kwerenda = mysqli_query($polaczenie, "SELECT * FROM paliwa ORDER BY id DESC LIMIT 3;");
$json = array();
while($row = mysqli_fetch_assoc($kwerenda)) {
$json [] = $row;
};
echo json_encode($json);
?>

20
php/post.php Normal file
View File

@ -0,0 +1,20 @@
<?php
$dbhost = "172.18.0.3";
$dbuser = "root";
$dbpass = "secret";
$dbname = "db";
$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$rawData = file_get_contents('php://input');
$data = json_decode($rawData, true);
$kw = "INSERT INTO paliwa (nazwa, cena) VALUES ('{$data['nazwa']}', {$data['cena']} );";
echo json_encode($kw);
$ans = mysqli_query($con, $kw);
?>

4
scr/__post Normal file
View File

@ -0,0 +1,4 @@
curl -X POST http://localhost:8080/p14.11/php/post.php \
-H "Content-Type: application/json" \
-d '{"nazwa": "Diesel", "cena": 5.25}'