forked from johno/pixyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajaxify_content_form.html
30 lines (25 loc) · 1.08 KB
/
ajaxify_content_form.html
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
<script>
var contactForm = document.querySelector('form'),
inputEmail = contactForm.querySelector('[name="email"]'),
textAreaMessage = contactForm.querySelector('[name="content"]'),
sendButton = contactForm.querySelector('button');
sendButton.addEventListener('click', function(event){
event.preventDefault();
sendButton.innerHTML = '{{ site.text.contact.ajax.sending | default: "sending..." }}';
var xhr = new XMLHttpRequest();
xhr.open('POST', '//formspree.io/{{ site.email }}', true);
xhr.setRequestHeader("Accept", "application/json")
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xhr.send(
"email=" + inputEmail.value +
"&message=" + textAreaMessage.value);
xhr.onloadend = function (res) {
if (res.target.status === 200){
sendButton.innerHTML = '{{ site.text.contact.ajax.sent | default: "Message sent!" }}';
}
else {
sendButton.innerHTML = '{{ site.text.contact.ajax.error | default: "Error!" }}';
}
}
});
</script>