Add confirmation modal + honeypot to contact form
- Submit now opens a 'Confirm your message' review dialog (reuses the site's dl-modal styles) showing name/company/email/message for a final check before anything is sent - Confirm & send performs the actual POST; Go back & edit / X / overlay click / Escape all return to the form with values intact - Extra bot defenses: hidden honeypot field (name=website) and a 2-second time-trap — bots that trip either get a fake success message and nothing is sent to the API - Payload to /api/contact unchanged
This commit is contained in:
parent
398711e7bf
commit
1ce0a12618
130
index.html
130
index.html
@ -774,6 +774,45 @@
|
||||
margin-left: auto;
|
||||
}
|
||||
}
|
||||
|
||||
.cf-review {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
background: rgba(2, 6, 23, 0.6);
|
||||
border: 1px solid rgba(148, 163, 184, 0.15);
|
||||
border-radius: 1.25rem;
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
.cf-row {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.cf-label {
|
||||
font-size: 0.7rem;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.12em;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.cf-value {
|
||||
color: #e2e8f0;
|
||||
font-size: 1rem;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.hp-field {
|
||||
position: absolute !important;
|
||||
left: -9999px !important;
|
||||
width: 1px !important;
|
||||
height: 1px !important;
|
||||
opacity: 0 !important;
|
||||
pointer-events: none !important;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
@ -1074,6 +1113,8 @@
|
||||
</div>
|
||||
<div class="lg:col-span-7 bg-slate-950 rounded-3xl p-10">
|
||||
<form id="contact-form" class="space-y-8">
|
||||
<input type="text" name="website" value="" tabindex="-1" autocomplete="off"
|
||||
class="hp-field" aria-hidden="true">
|
||||
<div class="grid grid-cols-2 gap-6">
|
||||
<div>
|
||||
<label class="block text-sm text-slate-400 mb-2">Your name</label>
|
||||
@ -1112,6 +1153,42 @@
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- CONTACT CONFIRM MODAL -->
|
||||
<div id="confirmModal" class="dl-modal-overlay" onclick="if(event.target===this)closeConfirmModal()">
|
||||
<div class="dl-modal-card" style="max-width: 560px;">
|
||||
<div class="dl-modal-header">
|
||||
<div>
|
||||
<h2
|
||||
style="font-family:'Space Grotesk',sans-serif; font-size:1.65rem; font-weight:600; color:#f8fafc; margin:0; line-height:1.3;">
|
||||
Confirm your message</h2>
|
||||
<p style="color:#6ee7b7; font-size:0.9rem; margin-top:0.4rem;">Quick check before it goes out —
|
||||
everything look right?</p>
|
||||
</div>
|
||||
<button class="dl-modal-close" onclick="closeConfirmModal()" aria-label="Close">×</button>
|
||||
</div>
|
||||
<div class="dl-modal-body" style="padding: 1.75rem 2.5rem 2rem;">
|
||||
<div class="cf-review">
|
||||
<div class="cf-row"><span class="cf-label">Name</span><span id="cf-name"
|
||||
class="cf-value"></span></div>
|
||||
<div class="cf-row"><span class="cf-label">Company</span><span id="cf-company"
|
||||
class="cf-value"></span></div>
|
||||
<div class="cf-row"><span class="cf-label">Email</span><span id="cf-email"
|
||||
class="cf-value"></span></div>
|
||||
<div class="cf-row"><span class="cf-label">Message</span><span id="cf-message"
|
||||
class="cf-value"></span></div>
|
||||
</div>
|
||||
<div class="flex flex-col sm:flex-row gap-4 mt-8">
|
||||
<button type="button" onclick="closeConfirmModal()"
|
||||
class="flex-1 py-4 border border-slate-600 hover:border-slate-400 text-slate-300 hover:text-white font-semibold text-lg rounded-2xl transition-colors">Go
|
||||
back & edit</button>
|
||||
<button type="button" id="cf-send-btn"
|
||||
class="flex-1 py-4 bg-emerald-400 hover:bg-emerald-300 text-slate-950 font-semibold text-lg rounded-2xl transition-colors">Confirm
|
||||
& send</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- DOWNLOAD MODAL -->
|
||||
<div id="downloadModal" class="dl-modal-overlay" onclick="if(event.target===this)closeDownloadModal()">
|
||||
<div class="dl-modal-card">
|
||||
@ -2103,10 +2180,31 @@
|
||||
}
|
||||
}
|
||||
|
||||
let cfPendingData = null;
|
||||
|
||||
function openConfirmModal() {
|
||||
document.getElementById('confirmModal').classList.add('active');
|
||||
document.body.style.overflow = 'hidden';
|
||||
}
|
||||
|
||||
function closeConfirmModal() {
|
||||
document.getElementById('confirmModal').classList.remove('active');
|
||||
document.body.style.overflow = '';
|
||||
}
|
||||
|
||||
function initContactForm() {
|
||||
const form = document.getElementById('contact-form');
|
||||
const successDiv = document.getElementById('success-message');
|
||||
form.addEventListener('submit', async function (e) {
|
||||
let formLoadedAt = Date.now();
|
||||
|
||||
const showSuccess = () => {
|
||||
successDiv.classList.remove('hidden');
|
||||
form.reset();
|
||||
formLoadedAt = Date.now();
|
||||
setTimeout(() => { successDiv.classList.add('hidden'); }, 6000);
|
||||
};
|
||||
|
||||
form.addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
const data = {
|
||||
name: form.name.value.trim(),
|
||||
@ -2115,7 +2213,25 @@
|
||||
message: form.message.value.trim()
|
||||
};
|
||||
if (!data.name || !data.email || !data.message) { alert('Please fill in all required fields.'); return; }
|
||||
const btn = form.querySelector('button[type="submit"]');
|
||||
|
||||
// Anti-bot: honeypot + time-trap. Humans never fill the hidden field
|
||||
// and never submit in under 2s — bots that do get a fake success, nothing is sent.
|
||||
const honeypot = form.website ? form.website.value.trim() : '';
|
||||
if (honeypot || (Date.now() - formLoadedAt) < 2000) { showSuccess(); return; }
|
||||
|
||||
cfPendingData = data;
|
||||
document.getElementById('cf-name').textContent = data.name;
|
||||
document.getElementById('cf-company').textContent = data.company || '—';
|
||||
document.getElementById('cf-email').textContent = data.email;
|
||||
document.getElementById('cf-message').textContent = data.message;
|
||||
openConfirmModal();
|
||||
document.getElementById('cf-send-btn').focus();
|
||||
});
|
||||
|
||||
document.getElementById('cf-send-btn').addEventListener('click', async function () {
|
||||
if (!cfPendingData) return;
|
||||
const data = cfPendingData;
|
||||
const btn = this;
|
||||
const originalText = btn.textContent;
|
||||
btn.disabled = true; btn.textContent = 'Sending...';
|
||||
try {
|
||||
@ -2124,11 +2240,15 @@
|
||||
});
|
||||
const result = await response.json();
|
||||
if (response.ok && result.success) {
|
||||
successDiv.classList.remove('hidden'); form.reset();
|
||||
setTimeout(() => { successDiv.classList.add('hidden'); }, 6000);
|
||||
closeConfirmModal();
|
||||
showSuccess();
|
||||
} else { alert(result.message || 'Something went wrong. Please try again.'); }
|
||||
} catch (error) { alert('Network error. Please check your connection and try again.'); }
|
||||
finally { btn.disabled = false; btn.textContent = originalText; }
|
||||
finally { btn.disabled = false; btn.textContent = originalText; cfPendingData = null; }
|
||||
});
|
||||
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Escape' && document.getElementById('confirmModal').classList.contains('active')) closeConfirmModal();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user