-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.js
39 lines (35 loc) · 934 Bytes
/
contact.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(function()
{
window.addEventListener('load', main, { once : true });
function main()
{
let form = document.forms.contactForm;
let messageNode = document.querySelector('.message');
document.forms.contactForm.addEventListener('submit', sendEmail);
async function sendEmail(event)
{
event.preventDefault();
try
{
await fetch('/sendEmail.php',
{
method : 'POST',
body : JSON.stringify(Object.fromEntries(new FormData(form))),
headers :
{
'Content-Type': 'application/json; charset=utf-8'
}
});
form.reset();
messageNode.classList.add('success');
messageNode.textContent = 'Your message has been sent successfully. We shall reach out to you shortly.';
}
catch (e)
{
messageNode.classList.add('error');
messageNode.textContent = 'There was an error sending your message. Please try again later.';
console.error(e);
}
}
}
})();