-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
37 lines (30 loc) · 1.02 KB
/
app.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
const contactForm = document.querySelector('.contact-form');
let yourName = document.getElementById('name');
let email = document.getElementById('email');
let subject = document.getElementById('subject');
let message = document.getElementById('message');
contactForm.addEventListener('submit', (e) => {
e.preventDefault(); // refreshed the form after submit
let formData = {
name: yourName.value,
email: email.value,
subject: subject.value,
message: message.value
}
let xhr = new XMLHttpRequest();
xhr.open('POST', '/');
xhr.setRequestHeader('content-type', 'application/json');
xhr.onload = function() {
console.log(xhr.responseText);
if(xhr.responseText == 'success') {
alert('Email sent');
yourName.value = '';
email.value = '';
subject.value = '';
message.value = '';
} else {
alert('Something is wrong')
}
}
xhr.send(JSON.stringify(formData));
})