inf03-06.2022-z2/js/script.js

55 lines
1.6 KiB
JavaScript

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");
}
});
document.getElementById("oblicz").addEventListener("click", () => {
const ile_litrow = document.getElementById("ile_litrow").value
const cena_paliwa = document.getElementById("cena").innerHTML
console.log(ile_litrow)
console.log(cena_paliwa)
const wynik = ile_litrow * cena_paliwa
console.log(wynik)
document.getElementById("wynik").innerHTML = wynik
})