inf03-06.2022-z2/js/get.js

43 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

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() {
fetch('php/get.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));
2024-11-08 11:41:12 +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");
}
});