inf03-06.2022-z2/js/script.js

43 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2024-11-08 13:44:15 +00:00
const fuelType = document.getElementById('fuel-type');
const cena = document.getElementById("cena");
let usersData = [];
2024-11-08 11:41:12 +00:00
document.addEventListener('DOMContentLoaded', function() {
2024-11-08 13:44:15 +00:00
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);
});
2024-11-08 11:41:12 +00:00
2024-11-08 13:44:15 +00:00
console.log(data);
})
.catch(error => console.error('Error fetching data:', error));
2024-11-08 11:41:12 +00:00
});
2024-11-08 13:44:15 +00:00
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");
}
});