37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
const contactForm = document.getElementById('contactForm');
|
|
if (contactForm) {
|
|
contactForm.addEventListener('submit', function (event) {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
|
|
if (contactForm.checkValidity() === false) {
|
|
contactForm.classList.add('was-validated');
|
|
return;
|
|
}
|
|
|
|
const formData = new FormData(contactForm);
|
|
const notification = document.getElementById('form-notification');
|
|
|
|
fetch('contact.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
notification.innerHTML = `<div class="alert alert-success">${data.message}</div>`;
|
|
contactForm.reset();
|
|
contactForm.classList.remove('was-validated');
|
|
} else {
|
|
notification.innerHTML = `<div class="alert alert-danger">${data.message}</div>`;
|
|
}
|
|
})
|
|
.catch(error => {
|
|
notification.innerHTML = `<div class="alert alert-danger">An error occurred. Please try again.</div>`;
|
|
});
|
|
});
|
|
}
|
|
});
|