1<form id="javascript_form">
2 <input type="text" name="subject" placeholder="Subject" />
3 <textarea name="text" placeholder="Message"></textarea>
4 <input type="submit" id="js_send" value="Send" />
5</form>
6
7<script>
8
9 //update this with your js_form selector
10 var form_id_js = "javascript_form";
11
12 var data_js = {
13 "access_token": "{your access token}" // sent after you sign up
14 };
15
16 function js_onSuccess() {
17 // remove this to avoid redirect
18 window.location = window.location.pathname + "?message=Email+Successfully+Sent%21&isError=0";
19 }
20
21 function js_onError(error) {
22 // remove this to avoid redirect
23 window.location = window.location.pathname + "?message=Email+could+not+be+sent.&isError=1";
24 }
25
26 var sendButton = document.getElementById("js_send");
27
28 function js_send() {
29 sendButton.value='Sending…';
30 sendButton.disabled=true;
31 var request = new XMLHttpRequest();
32 request.onreadystatechange = function() {
33 if (request.readyState == 4 && request.status == 200) {
34 js_onSuccess();
35 } else
36 if(request.readyState == 4) {
37 js_onError(request.response);
38 }
39 };
40
41 var subject = document.querySelector("#" + form_id_js + " [name='subject']").value;
42 var message = document.querySelector("#" + form_id_js + " [name='text']").value;
43 data_js['subject'] = subject;
44 data_js['text'] = message;
45 var params = toParams(data_js);
46
47 request.open("POST", "https://postmail.invotes.com/send", true);
48 request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
49
50 request.send(params);
51
52 return false;
53 }
54
55 sendButton.onclick = js_send;
56
57 function toParams(data_js) {
58 var form_data = [];
59 for ( var key in data_js ) {
60 form_data.push(encodeURIComponent(key) + "=" + encodeURIComponent(data_js[key]));
61 }
62
63 return form_data.join("&");
64 }
65
66 var js_form = document.getElementById(form_id_js);
67 js_form.addEventListener("submit", function (e) {
68 e.preventDefault();
69 });
70</script>
71