66 lines
2.5 KiB
JavaScript
66 lines
2.5 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function() {
|
|
const contactForm = document.getElementById('contactForm');
|
|
const toastContainer = document.getElementById('toast-container');
|
|
|
|
if (contactForm) {
|
|
contactForm.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
const submitBtn = contactForm.querySelector('button[type="submit"]');
|
|
const originalBtnText = submitBtn.innerHTML;
|
|
submitBtn.disabled = true;
|
|
submitBtn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Sending...';
|
|
|
|
const formData = new FormData(contactForm);
|
|
|
|
fetch('api/leads.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
showToast(data.message, 'success');
|
|
contactForm.reset();
|
|
} else {
|
|
showToast(data.error || 'Something went wrong.', 'danger');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
showToast('An error occurred. Please try again.', 'danger');
|
|
})
|
|
.finally(() => {
|
|
submitBtn.disabled = false;
|
|
submitBtn.innerHTML = originalBtnText;
|
|
});
|
|
});
|
|
}
|
|
|
|
function showToast(message, type) {
|
|
const toastId = 'toast-' + Date.now();
|
|
const toastHTML = `
|
|
<div id="${toastId}" class="toast show" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="toast-header bg-dark text-white border-bottom border-secondary">
|
|
<strong class="me-auto">${type === 'success' ? 'Success' : 'Error'}</strong>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
<div class="toast-body">
|
|
${message}
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
const toastElement = document.createElement('div');
|
|
toastElement.innerHTML = toastHTML;
|
|
toastContainer.appendChild(toastElement);
|
|
|
|
setTimeout(() => {
|
|
const toast = document.getElementById(toastId);
|
|
if (toast) {
|
|
toast.classList.remove('show');
|
|
setTimeout(() => toast.remove(), 500);
|
|
}
|
|
}, 5000);
|
|
}
|
|
}); |