From 2d1185ff074c75125d7904d5a91b70a1ef71ee32 Mon Sep 17 00:00:00 2001 From: u1 Date: Thu, 14 Nov 2024 14:37:28 +0100 Subject: [PATCH] u --- js/post.js | 36 ++++++++++++++++++++++++++++++++++++ php/get.php | 15 +++++++++++++++ php/post.php | 20 ++++++++++++++++++++ scr/__post | 4 ++++ 4 files changed, 75 insertions(+) create mode 100644 js/post.js create mode 100644 php/get.php create mode 100644 php/post.php create mode 100644 scr/__post diff --git a/js/post.js b/js/post.js new file mode 100644 index 0000000..dfea225 --- /dev/null +++ b/js/post.js @@ -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); + } +}); + diff --git a/php/get.php b/php/get.php new file mode 100644 index 0000000..e83f709 --- /dev/null +++ b/php/get.php @@ -0,0 +1,15 @@ + diff --git a/php/post.php b/php/post.php new file mode 100644 index 0000000..b8da1fb --- /dev/null +++ b/php/post.php @@ -0,0 +1,20 @@ + diff --git a/scr/__post b/scr/__post new file mode 100644 index 0000000..ae82c89 --- /dev/null +++ b/scr/__post @@ -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}' +