From 67a4c8818346653a250006737b550fddb1a88b0c Mon Sep 17 00:00:00 2001 From: Andreas Hnida Date: Tue, 27 Feb 2024 10:38:47 +0000 Subject: [PATCH] =?UTF-8?q?erstellt=20f=C3=BCr=20=20async=20anfrage=20an?= =?UTF-8?q?=20planio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/js/kontaktformular.js | 113 +++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 assets/js/kontaktformular.js diff --git a/assets/js/kontaktformular.js b/assets/js/kontaktformular.js new file mode 100644 index 0000000..25a49c0 --- /dev/null +++ b/assets/js/kontaktformular.js @@ -0,0 +1,113 @@ +window.onload = function () { + + const FORMDEBUG = false; + const btn = document.getElementById('bestellformular-btn'); + + // initieiere Zeitmessung zur Botprevention + var startTime = Date.now(); + + // Messe ob mit der Seite agiert wird + var userInteracted = false; + + function setUserInteracted() { + var timeSpent = (Date.now() - startTime) / 1000; // Zeit in Sekunden + if (timeSpent > 5) { + btn.disabled = false; + } + userInteracted = true; + } + setTimeout(function () { + if (userInteracted) { + btn.disabled = false; + } + }, 5000); + // Eventlistener für Interaktionen - setzt userInteracted auf true bei Interaktion + document.addEventListener("mousedown", setUserInteracted); + document.addEventListener("touchstart", setUserInteracted); + document.addEventListener("keydown", setUserInteracted); + + document.getElementById('formular').addEventListener('submit', function (e) { + + e.preventDefault(); + + const form = e.target; + const notification = document.getElementById('notification'); + const zsrTooltip = document.getElementById('zsr-tooltip'); + + // Spinner und button disabled anzeigen + + + + var endTime = Date.now(); + var timeSpent = (endTime - startTime) / 1000; // Zeit in Sekunden + + // Setze die Werte für die Botvalidierung zum Auswerten in PHP + document.getElementById("age").value = timeSpent; + document.getElementById("hobbies").value = userInteracted ? "true" : "false"; + + btn.innerHTML = ` + + + + + + ` + + btn.disabled = true; + + if (FORMDEBUG) { + console.log("userInteracted: " + userInteracted); + console.log("timeSpent: " + timeSpent); + console.log("hobbies: " + document.getElementById("hobbies").value); + console.log("age: " + document.getElementById("age").value); + console.log("verify_email(honeypot): " + document.getElementById("verify_email").value); + } + + // Konvertiere das JSON-Objekt in einen String, um es zu senden + const formData = new FormData(form); + const formDataEncoded = new URLSearchParams(formData).toString(); + const formURL = form.action + '.json' + + fetch(formURL, { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', // Wichtig für die Vermeidung von CORS-Problemen + }, + body: formDataEncoded, + }) + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); + }) + .then(data => { + // Erfolgsnachricht anzeigen + // TODO Nachricht anpassen wenn französische Version + notification.textContent = 'Anfrage erfolgreich versendet.'; + btn.className = 'submitbutton text-white mx-auto submit-after-valid-captchaaaa fadeOut'; + setTimeout(() => { + btn.style.visibility = 'hidden'; + btn.style.display = 'none'; + notification.style.visibility = 'visible'; + notification.style.display = 'block'; + notification.classList.remove('fadeIn'); // Remove fadeIn class + void notification.offsetWidth; + notification.className = 'bg-green-500 text-white px-4 py-2 rounded block fadeIn'; + }, 1000); + // setTimeout(() => notification.className = 'bg-green-500 text-white px-4 py-2 rounded hidden', 5000); // Benachrichtigung nach 5 Sekunden ausblenden + }) + .catch((error) => { + // Fehlermeldung anzeigen + notification.textContent = 'Fehler beim Senden der Nachricht.'; + console.log(error); + notification.className = 'bg-red-500 text-white px-4 py-2 rounded block'; + // setTimeout(() => notification.className = 'bg-red-500 text-white px-4 py-2 rounded hidden', 5000); // Benachrichtigung nach 5 Sekunden ausblenden + }); + }) +} \ No newline at end of file