From 708582f7780ac2ac3fadd112558d282252f179a4 Mon Sep 17 00:00:00 2001 From: Alok Date: Fri, 9 Aug 2024 16:20:37 +0530 Subject: [PATCH] Add form async scripts and div show/hide --- src/components/custom/LeadForm.astro | 21 +++++++++---- src/components/custom/NewsletterForm.astro | 34 +++++++++++++++++++++- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/components/custom/LeadForm.astro b/src/components/custom/LeadForm.astro index c8addf57..eae16b30 100644 --- a/src/components/custom/LeadForm.astro +++ b/src/components/custom/LeadForm.astro @@ -1,8 +1,11 @@ --- -let username = "user2s"; --- - +
+
- +
Message successfully sent!
-

Hey {username}!

+

Hey!

I have received your message, so keep an eye out on your mailbox to see if you have a reply. I'll try my best to get back to you within a day. @@ -100,6 +105,8 @@ let username = "user2s"; const form = event.target; const formData = new FormData(form); const errorDiv = document.querySelector('.error'); + const formDiv = document.querySelector('.formblock'); + const noticeDiv = document.querySelector('.notice'); try { const response = await fetch(form.action, { @@ -109,8 +116,12 @@ let username = "user2s"; if (response.ok) { const result = await response.json(); + console.log(result); + errorDiv.textContent = 'Message sent successfully!'; - errorDiv.style.color = 'green'; + formDiv.style.display = 'none'; + noticeDiv.style.display = 'block'; + } else { const error = await response.json(); errorDiv.textContent = `Error: ${error.message}`; diff --git a/src/components/custom/NewsletterForm.astro b/src/components/custom/NewsletterForm.astro index 794250b1..d0ae609a 100644 --- a/src/components/custom/NewsletterForm.astro +++ b/src/components/custom/NewsletterForm.astro @@ -45,7 +45,39 @@

- You have successfully registered for my newsletter! +
+ + \ No newline at end of file