65 lines
1.9 KiB
JavaScript
65 lines
1.9 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function () {
|
|
const params = new URLSearchParams(window.location.search);
|
|
if (params.get('contact_submitted') || params.get('contact_error')) {
|
|
const contactSection = document.getElementById('contact');
|
|
if (contactSection) {
|
|
contactSection.scrollIntoView({ behavior: 'smooth' });
|
|
}
|
|
}
|
|
});
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
const form = document.querySelector('.php-email-form');
|
|
if (!form) return;
|
|
|
|
const loading = document.createElement('div');
|
|
const errorMsg = document.createElement('div');
|
|
const successMsg = document.createElement('div');
|
|
|
|
loading.className = 'loading mt-3';
|
|
loading.textContent = 'Sending message...';
|
|
|
|
errorMsg.className = 'error-message mt-3';
|
|
errorMsg.style.display = 'none';
|
|
|
|
successMsg.className = 'sent-message mt-3';
|
|
successMsg.style.display = 'none';
|
|
|
|
form.appendChild(loading);
|
|
form.appendChild(errorMsg);
|
|
form.appendChild(successMsg);
|
|
|
|
loading.style.display = 'none';
|
|
|
|
form.addEventListener('submit', async function (e) {
|
|
e.preventDefault();
|
|
|
|
loading.style.display = 'block';
|
|
errorMsg.style.display = 'none';
|
|
successMsg.style.display = 'none';
|
|
|
|
const formData = new FormData(form);
|
|
|
|
try {
|
|
const response = await fetch(form.action, {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
|
|
const result = await response.json();
|
|
loading.style.display = 'none';
|
|
|
|
if (response.ok && result.success) {
|
|
successMsg.textContent = result.message || 'Your message was sent successfully.';
|
|
successMsg.style.display = 'block';
|
|
form.reset();
|
|
} else {
|
|
throw new Error(result.error || 'Submission failed.');
|
|
}
|
|
} catch (err) {
|
|
loading.style.display = 'none';
|
|
errorMsg.textContent = err.message || 'An unexpected error occurred.';
|
|
errorMsg.style.display = 'block';
|
|
}
|
|
});
|
|
});
|
|
|