This commit is contained in:
parent
616a1e2e68
commit
2d1185ff07
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
|
@ -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);
|
||||
|
||||
?>
|
|
@ -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);
|
||||
|
||||
?>
|
|
@ -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}'
|
||||
|
Loading…
Reference in New Issue