Skip to content

Commit

Permalink
Add form async scripts and div show/hide
Browse files Browse the repository at this point in the history
  • Loading branch information
theWhiteWulfy committed Aug 9, 2024
1 parent ff8ab42 commit 708582f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/components/custom/LeadForm.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
---
let username = "user2s";
---

<style>
.notice{
display: none;
}
</style>
<form
id="contact-form"
name="contact-form"
Expand All @@ -12,6 +15,7 @@ let username = "user2s";
method="post"
action="/api/leadform"
>
<div class="custom-block formblock">
<div class="form-group">
<label id="name-label" for="usrname">
Name
Expand Down Expand Up @@ -71,14 +75,15 @@ let username = "user2s";
<button id="saveForm" name="saveForm" class="btn submit" type="submit">
Send message
</button>
<div class="error"></div>
</div>

</div>


<div class="custom-block notice">
<div class="custom-block-heading">Message successfully sent!</div>
<div class="custom-block-body">
<p>Hey {username}!</p>
<p>Hey!</p>
<p>
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.
Expand All @@ -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, {
Expand All @@ -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}`;
Expand Down
34 changes: 33 additions & 1 deletion src/components/custom/NewsletterForm.astro
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,39 @@
</div>

<div class="custom-block-body">
You have successfully registered for my newsletter!

</div>
</form>
</div>

<script is:inline>
document.getElementById('newsletter-form').addEventListener('submit', async function(event) {
event.preventDefault(); // Prevent the default form submission

const form = event.target;
const formData = new FormData(form);
const errorDiv = document.querySelector('.custom-block-body');
const formDiv = document.querySelector('.form-group');
try {
const response = await fetch(form.action, {
method: form.method,
body: formData
});

if (response.ok) {
const result = await response.json();
console.log(result);
formDiv.style.display = 'none';
errorDiv.textContent = 'You have successfully registered for my newsletter!';

} else {
const error = await response.json();
errorDiv.textContent = `Error: ${error.message}`;
errorDiv.style.color = 'red';
}
} catch (error) {
errorDiv.textContent = `Error: ${error.message}`;
errorDiv.style.color = 'red';
}
});
</script>

0 comments on commit 708582f

Please sign in to comment.