Compare commits
2 Commits
2fbf0bb91d
...
2d1185ff07
Author | SHA1 | Date |
---|---|---|
u1 | 2d1185ff07 | |
u1 | 616a1e2e68 |
37
@
37
@
|
@ -1,37 +0,0 @@
|
|||
document.addEventListener('DOMContentLoaded', function() {
|
||||
|
||||
//const imieSelect = document.getElementById('imie');
|
||||
|
||||
let usersData = [];
|
||||
|
||||
imieSelect.addEventListener('focus', function() {
|
||||
fetch('php/gget.php')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
/*
|
||||
usersData = data;
|
||||
imieSelect.innerHTML = "";
|
||||
data.forEach(dane => {
|
||||
const option = document.createElement('option');
|
||||
option.value = dane.id;
|
||||
option.textContent = dane.imie + " " + dane.nazwisko;
|
||||
imieSelect.appendChild(option);
|
||||
});
|
||||
*/
|
||||
|
||||
console.log(data);
|
||||
})
|
||||
.catch(error => console.error('Error fetching data:', error));
|
||||
});
|
||||
/*
|
||||
imieSelect.addEventListener('change', function() {
|
||||
const selectedId = imieSelect.value;
|
||||
const selectedPerson = usersData.find(dane => dane.id == selectedId);
|
||||
if (selectedPerson && selectedPerson.photo) {
|
||||
photoDisplay.src = 'data:image/jpeg;base64,' + selectedPerson.photo;
|
||||
} else {
|
||||
photoDisplay.src = '';
|
||||
}
|
||||
});
|
||||
*/
|
||||
});
|
13
index.html
13
index.html
|
@ -6,7 +6,8 @@
|
|||
<title>Całodobowa stacja paliw</title>
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
|
||||
<script type="module" src="js/script.js"></script>
|
||||
<script type="module" src="js/get.js"></script>
|
||||
<script type="module" src="js/post.js"></script>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
|
@ -43,6 +44,16 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div>
|
||||
<h2>Orientacyjny koszt paliwa</h2>
|
||||
|
||||
<input type="text" id="fuel" name="name">
|
||||
<input type="text" id="price" name="price">
|
||||
<button type="button" id="btn">Oblicz</button>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="footer">
|
||||
Stronę opracował: 000000000000
|
||||
</div>
|
||||
|
|
47
js/get.js
47
js/get.js
|
@ -1,35 +1,42 @@
|
|||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const imieSelect = document.getElementById('imie');
|
||||
const photoDisplay = document.createElement('img');
|
||||
photoDisplay.style.width = "200px";
|
||||
photoDisplay.style.height = "auto";
|
||||
document.getElementById('prawy').appendChild(photoDisplay);
|
||||
const fuelType = document.getElementById('fuel-type');
|
||||
|
||||
const cena = document.getElementById("cena");
|
||||
|
||||
let usersData = [];
|
||||
|
||||
imieSelect.addEventListener('focus', function() {
|
||||
fetch('get.php')
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
fetch('php/get.php')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
usersData = data;
|
||||
imieSelect.innerHTML = "";
|
||||
data.forEach(dane => {
|
||||
|
||||
data.forEach(row => {
|
||||
const option = document.createElement('option');
|
||||
option.value = dane.id;
|
||||
option.textContent = dane.imie + " " + dane.nazwisko;
|
||||
imieSelect.appendChild(option);
|
||||
option.value = row["nazwa"];
|
||||
option.textContent = row["nazwa"];
|
||||
fuelType.appendChild(option);
|
||||
|
||||
console.log(row);
|
||||
});
|
||||
|
||||
console.log(data);
|
||||
})
|
||||
.catch(error => console.error('Error fetching data:', error));
|
||||
});
|
||||
|
||||
imieSelect.addEventListener('change', function() {
|
||||
const selectedId = imieSelect.value;
|
||||
const selectedPerson = usersData.find(dane => dane.id == selectedId);
|
||||
if (selectedPerson && selectedPerson.photo) {
|
||||
photoDisplay.src = 'data:image/jpeg;base64,' + selectedPerson.photo;
|
||||
fuelType.addEventListener('change', function() {
|
||||
console.log("Zmiana wybranego paliwa");
|
||||
const selectedId = fuelType.value;
|
||||
|
||||
const selectedFuel = usersData.find(dane => dane.nazwa === selectedId);
|
||||
|
||||
cena.textContent = selectedFuel["cena"];
|
||||
console.log("mati");
|
||||
if (selectedFuel) {
|
||||
const cena = selectedFuel.cena;
|
||||
console.log("Cena wybranego paliwa:", cena);
|
||||
} else {
|
||||
photoDisplay.src = '';
|
||||
console.error("Nie znaleziono ceny dla wybranego paliwa");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
42
js/script.js
42
js/script.js
|
@ -1,42 +0,0 @@
|
|||
const fuelType = document.getElementById('fuel-type');
|
||||
|
||||
const cena = document.getElementById("cena");
|
||||
|
||||
let usersData = [];
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
fetch('php/gget.php')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
usersData = data;
|
||||
|
||||
data.forEach(row => {
|
||||
const option = document.createElement('option');
|
||||
option.value = row["nazwa"];
|
||||
option.textContent = row["nazwa"];
|
||||
fuelType.appendChild(option);
|
||||
|
||||
console.log(row);
|
||||
});
|
||||
|
||||
console.log(data);
|
||||
})
|
||||
.catch(error => console.error('Error fetching data:', error));
|
||||
});
|
||||
|
||||
fuelType.addEventListener('change', function() {
|
||||
console.log("Zmiana wybranego paliwa");
|
||||
const selectedId = fuelType.value;
|
||||
|
||||
const selectedFuel = usersData.find(dane => dane.nazwa === selectedId);
|
||||
|
||||
cena.textContent = selectedFuel["cena"];
|
||||
console.log("mati");
|
||||
if (selectedFuel) {
|
||||
const cena = selectedFuel.cena;
|
||||
console.log("Cena wybranego paliwa:", cena);
|
||||
} else {
|
||||
console.error("Nie znaleziono ceny dla wybranego paliwa");
|
||||
}
|
||||
});
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
$polaczenie = mysqli_connect("172.19.0.2", "root", "secret", "db");
|
||||
$polaczenie = mysqli_connect("172.18.0.3", "root", "secret", "db");
|
||||
|
||||
$kwerenda = mysqli_query($polaczenie, "SELECT * FROM paliwa ORDER BY id DESC LIMIT 3;");
|
||||
|
|
@ -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