2022-08-24 15:13:39 +02:00
|
|
|
const contactFormAjaxUrl = '/php/contact_form.php';
|
2022-06-16 13:02:42 +02:00
|
|
|
|
2022-08-24 15:27:20 +02:00
|
|
|
window.addEventListener('DOMContentLoaded', function() {
|
|
|
|
const contact_form = document.getElementsByClassName('content__contact_form')[0];
|
|
|
|
if (contact_form) {
|
|
|
|
contact_form.addEventListener('submit', wtf_submitContactForm, false);
|
|
|
|
wtf_startContactFormSession();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function wtf_startContactFormSession() {
|
2022-08-03 18:04:03 +02:00
|
|
|
let formData = new FormData();
|
|
|
|
formData.append('action', 'start_session');
|
2022-08-24 15:13:39 +02:00
|
|
|
fetch(contactFormAjaxUrl, {
|
2022-08-03 18:04:03 +02:00
|
|
|
method: 'POST',
|
|
|
|
mode: 'same-origin',
|
|
|
|
body: formData,
|
|
|
|
})
|
2022-08-24 15:15:08 +02:00
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Response was not OK');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.json();
|
|
|
|
})
|
2022-08-03 18:04:03 +02:00
|
|
|
.then(json => {
|
|
|
|
console.log(json);
|
|
|
|
})
|
2022-08-24 15:15:08 +02:00
|
|
|
.catch(error => {
|
|
|
|
console.error('Could not start the session:', error);
|
|
|
|
});
|
2022-08-24 15:27:20 +02:00
|
|
|
}
|
2022-08-03 18:04:03 +02:00
|
|
|
|
2022-08-24 15:27:20 +02:00
|
|
|
function wtf_submitContactForm(event) {
|
2022-06-16 13:02:42 +02:00
|
|
|
event.preventDefault();
|
2022-08-24 15:27:20 +02:00
|
|
|
|
|
|
|
const subject = document.getElementsByClassName('contact_form__subject')[0];
|
|
|
|
const message = document.getElementsByClassName('contact_form__message')[0];
|
|
|
|
const name = document.getElementsByClassName('contact_form__name')[0];
|
|
|
|
const email = document.getElementsByClassName('contact_form__email')[0];
|
|
|
|
const captcha = document.getElementsByClassName('contact_form__captcha')[0];
|
|
|
|
|
2022-06-16 13:02:42 +02:00
|
|
|
let formData = new FormData();
|
2022-08-03 18:04:03 +02:00
|
|
|
formData.append('action', 'handle_form');
|
2022-08-03 19:31:06 +02:00
|
|
|
formData.append('subject', subject.value);
|
2022-06-16 13:02:42 +02:00
|
|
|
formData.append('message', message.value);
|
|
|
|
formData.append('name', name.value);
|
|
|
|
formData.append('email', email.value);
|
|
|
|
|
|
|
|
// If some bot entered some value, return.
|
|
|
|
if (typeof captcha.value == 'undefined') {
|
|
|
|
formData.append('captcha', 'Nudelsuppe');
|
|
|
|
} else {
|
|
|
|
console.log('bot detected');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:13:39 +02:00
|
|
|
fetch(contactFormAjaxUrl, {
|
2022-06-16 13:02:42 +02:00
|
|
|
method: 'POST',
|
2022-08-03 18:04:03 +02:00
|
|
|
mode: 'same-origin',
|
2022-06-16 13:02:42 +02:00
|
|
|
body: formData,
|
|
|
|
})
|
|
|
|
.then(response => response.json())
|
2022-06-16 18:23:05 +02:00
|
|
|
.then(json => {
|
2022-08-24 15:27:20 +02:00
|
|
|
const feedback = document.getElementsByClassName('contact_form__feedback')[0];
|
2022-08-03 18:04:03 +02:00
|
|
|
console.log(json);
|
2022-06-16 18:23:05 +02:00
|
|
|
if (json.errors) {
|
2022-08-03 19:32:09 +02:00
|
|
|
feedback.classList.remove('--success');
|
2022-06-16 18:23:05 +02:00
|
|
|
feedback.classList.add('--error');
|
|
|
|
// Über errors iterieren und diese ausgeben (evtl. nur ersten Fehler ausgeben?)
|
|
|
|
let error_message = '';
|
|
|
|
json.errors.forEach(function(error){
|
|
|
|
/**
|
|
|
|
* Nur Zeilenumbrüche wenn mehrer Fehlermeldungen existieren,
|
|
|
|
* aber bei der letzten nicht.
|
|
|
|
*/
|
|
|
|
if (json.errors.length > 1) {
|
|
|
|
if (error == json.errors[json.errors.length - 1]) {
|
|
|
|
error_message = error_message + error;
|
|
|
|
} else {
|
|
|
|
error_message = error_message + error + '<br>';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error_message = error_message + error;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
feedback.innerHTML = error_message;
|
|
|
|
} else if (json.status == 'ok') {
|
2022-08-03 19:32:09 +02:00
|
|
|
feedback.classList.remove('--error');
|
2022-06-16 18:23:05 +02:00
|
|
|
feedback.classList.add('--success');
|
2022-08-17 21:56:57 +02:00
|
|
|
feedback.textContent = "Ihre Nachricht wurde erfolgreich ans Office geschickt.";
|
2022-06-16 18:23:05 +02:00
|
|
|
}
|
|
|
|
})
|
2022-06-16 16:52:20 +02:00
|
|
|
.catch(error => console.log(error));
|
2022-08-24 15:27:20 +02:00
|
|
|
}
|