From e30d313d987cfab61d317ce6edba20a3f4b47ed4 Mon Sep 17 00:00:00 2001 From: baiobelfer Date: Wed, 10 Sep 2025 11:35:22 +0000 Subject: [PATCH] add spread data from db to table in main view --- i1.html | 4 ++- js/script.js | 75 ++++++++++++++++++++++++++++------------------------ 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/i1.html b/i1.html index 7047556..5721596 100644 --- a/i1.html +++ b/i1.html @@ -4,6 +4,8 @@ Kursy komputerowe + +
@@ -17,7 +19,7 @@

Kursy komputerowe - programowanie

- +
diff --git a/js/script.js b/js/script.js index c253f06..cdccd18 100644 --- a/js/script.js +++ b/js/script.js @@ -1,37 +1,44 @@ document.addEventListener('DOMContentLoaded', function() { - document.getElementById('oblicz').addEventListener('click', function() { - // Pobieranie wybranych kursów - const wybraneKursy = document.querySelectorAll('input[name="kurs"]:checked'); - // Ceny kursów - const cenaReact = 1500; - const cenaJavaScript = 1200; - - // Obliczanie całkowitej kwoty - let calkowitaKwota = 0; - wybraneKursy.forEach(kurs => { - if (kurs.value === 'React.js') { - calkowitaKwota += cenaReact; - } else if (kurs.value === 'JavaScript') { - calkowitaKwota += cenaJavaScript; - } - }); - - // Pobieranie liczby rat - const iloscRat = parseInt(document.getElementById('raty').value); - // Pobieranie miasta - const miasto = document.getElementById('miasto').value; - - // Sprawdzanie poprawności danych - if (wybraneKursy.length === 0 || isNaN(iloscRat) || iloscRat <= 0) { - document.getElementById('wynik').textContent = 'Proszę wybrać przynajmniej jeden kurs i podać poprawną liczbę rat.'; - return; + console.log("ok"); + + const fetchData = () => { + return fetch('./php/get.php') + .then(response => { + if (!response.ok) { + throw new Error('Błąd pobierania danych'); + } + return response.json(); + }) + .then(data => { + console.log("Dane załadowane:", data); + renderTable(data); // Wywołaj funkcję renderującą tabelę + }) + .catch(error => { + console.error("Błąd:", error); + }); + }; + + // Funkcja do renderowania danych w tabeli + const renderTable = (data) => { + const table = document.getElementById('tab'); + + // Usuń wszystkie rzędy po nagłówku (czyli od drugiego) + while (table.rows.length > 1) { + table.deleteRow(1); } - - // Obliczanie miesięcznej raty - const rata = calkowitaKwota / iloscRat; - - // Wyświetlanie wyniku - document.getElementById('wynik').textContent = - `Kurs odbędzie się w ${miasto}. Koszt całkowity: ${calkowitaKwota} zł. Płacisz ${iloscRat} rat po ${rata.toFixed(2)} zł`; - }); + + // Dodaj nowe wiersze dla każdego kursu + data.forEach(row => { + const tr = document.createElement('tr'); + tr.innerHTML = ` + + + + `; + table.appendChild(tr); + }); + }; + + // Uruchom pobieranie danych + fetchData(); });
Kurs Czas trwania${row.kurs}${row.czas_trwania}${row.cena}