You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
portio-hugo/assets/js/form-handler.js

41 lines
1.2 KiB

window.addEventListener("DOMContentLoaded", function () {
var form = document.getElementById("contact-form");
var button = document.getElementById("contact-form-button");
var status = document.getElementById("contact-form-status");
function success() {
form.reset();
button.style = "display: none ";
status.innerHTML = "Danke! Ihre Nachricht wurde gesendet.";
}
function error() {
status.innerHTML = "Upps! Ein Fehler ist aufgetreten.";
}
// handle the form submission event
if (form != null) {
form.addEventListener("submit", function (ev) {
ev.preventDefault();
var data = new FormData(form);
ajax(form.method, form.action, data, success, error);
});
}
});
// helper function for sending an AJAX request
function ajax(method, url, data, success, error) {
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("Accept", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if (xhr.status === 200) {
success(xhr.response, xhr.responseType);
} else {
error(xhr.status, xhr.response, xhr.responseType);
}
};
xhr.send(data);
}