quick fix 2
This commit is contained in:
60
webserver/web/sugestie.js
Normal file
60
webserver/web/sugestie.js
Normal file
@ -0,0 +1,60 @@
|
||||
// Zmieniamy 'sugestionForm' i 'submit' na 'sendButton' i 'click'
|
||||
document.getElementById('sendButton').addEventListener('click', async function(event) {
|
||||
// Nie musimy już wywoływać event.preventDefault(), ponieważ to nie jest submit formularza
|
||||
|
||||
const statusDiv = document.getElementById('status');
|
||||
statusDiv.textContent = 'Wysyłanie danych...';
|
||||
statusDiv.style.color = 'orange';
|
||||
|
||||
// 1. Pobranie zmiennych z pól wejściowych
|
||||
const zmienna1 = document.getElementById('sugestia').value;
|
||||
const zmienna2 = document.getElementById('link').value;
|
||||
|
||||
// Weryfikacja obowiązkowej zmiennej 1
|
||||
if (!zmienna1.trim()) {
|
||||
statusDiv.textContent = 'BŁĄD: Zmienna 1 jest obowiązkowa!';
|
||||
statusDiv.style.color = 'red';
|
||||
return; // Zatrzymuje wysyłanie
|
||||
}
|
||||
|
||||
// 2. Przygotowanie danych jako obiekt JavaScript (JSON)
|
||||
const dataToSend = {
|
||||
zmienna1: zmienna1,
|
||||
zmienna2: zmienna2
|
||||
};
|
||||
|
||||
// 3. Wysłanie danych do API za pomocą Fetch API
|
||||
try {
|
||||
const response = await fetch('/api/sugestie', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(dataToSend)
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (response.ok && result.success) {
|
||||
// Sukces
|
||||
statusDiv.textContent = 'Sukces! Twoje dane nigdy nie będą przeczytane';
|
||||
statusDiv.style.color = 'green';
|
||||
|
||||
// Opcjonalne: Wyczyść pola po sukcesie
|
||||
document.getElementById('sugestia').value = '';
|
||||
document.getElementById('link').value = '';
|
||||
|
||||
} else {
|
||||
// Błąd
|
||||
const errorMessage = result.error || 'Nieznany błąd zapisu.';
|
||||
statusDiv.textContent = 'BŁĄD: ' + errorMessage;
|
||||
statusDiv.style.color = 'red';
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
// Błąd sieci
|
||||
statusDiv.textContent = 'BŁĄD POŁĄCZENIA: Nie można połączyć się z serwerem Flask.';
|
||||
statusDiv.style.color = 'red';
|
||||
console.error('Fetch error:', error);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user