Skip to content

Commit

Permalink
add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
theWhiteWulfy committed Aug 8, 2024
1 parent 235e19e commit 35e81de
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/components/custom/LeadForm.astro
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,33 @@ let username = "user2s";
</div>
</div>
</form>

<script is:inline>
document.getElementById('contact-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('.error');

try {
const response = await fetch(form.action, {
method: form.method,
body: formData
});

if (response.ok) {
const result = await response.json();
errorDiv.textContent = 'Message sent successfully!';
errorDiv.style.color = 'green';
} 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 35e81de

Please sign in to comment.