-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-form.html
128 lines (102 loc) · 3.65 KB
/
example-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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example Form</title>
<style>
form#commentform input.missing,
form#commentform textarea.missing {
border: 2px solid red !important;
}
</style>
</head>
<body>
<form action="" method="post" id="commentform">
<fieldset id="commentfields">
<div>
<input name="cmt_slug" type="hidden" value="{{ slug }}">
<input name="comment-site" type="hidden" value="{{ site.url }}">
</div>
<div>
<label for="comment-name">Name/Alias <span>(needed, will be displayed)</span></label>
<input type="text" name="cmt_name" id="comment-name" class="required" placeholder="Name"/>
</div>
<div>
<label for="comment-email">E-Mail <span>(needed, will not be displayed)</span></label>
<input type="text" name="cmt_email" id="comment-email" class="required" placeholder="me@somewhere.net"/>
</div>
<div>
<label for="comment-message">Comment <span>(needed, will be moderated)</span></label>
<textarea name="cmt_message" id="comment-message" class="required"
placeholder="Write a comment."></textarea>
</div>
<div>
<label for="comment-url">Website <span>(optional, will be displayed)</span></label>
<input type="text" name="cmt_url" id="comment-url" placeholder="https://www.somewhere.net"/>
</div>
<div>
<p id="commentstatus"></p>
</div>
<div>
<button type="submit" id="commentbutton">Post comment</button>
</div>
</fieldset>
</form>
<script>
async function sendForm(url, formData) {
const response = await fetch("{{ site.comments.receiver }}", {
method: 'POST',
mode: 'cors',
redirect: 'follow',
body: formData
})
if (response.status != 201) {
throw Error(response.statusText);
}
return response
}
window.addEventListener("DOMContentLoaded", function() {
var form = document.getElementById('commentform')
form.addEventListener('submit', function(e) {
e.preventDefault()
var status = document.getElementById('commentstatus')
status.innerText = ''
missing = false
inputs = document.forms["commentform"].getElementsByClassName("required");
for (item of inputs) {
if (item.value.length < 1) {
item.classList.add("missing")
missing = true
} else {
item.classList.remove("missing")
}
}
if (missing) {
status.innerText = 'The colored fields feel so lonely without a value.'
return
}
confirm_text = "Really post comment"
var button = document.getElementById('commentbutton')
if (button.innerText.toLowerCase() == "Post comment".toLowerCase()) {
button.innerText = confirm_text
return
}
button.innerText = 'Posting …'
button.disabled = true
sendForm("{{ site.comments.receiver }}", new FormData(form))
.then(response => response.json())
.then(response => {
console.log(response)
pr = response["pr"]
status.innerText = "The comment #" + pr + "will be moderated. This may take A Moment™."
button.style.visibility = "hidden"
})
.catch(error => {
status.innerText = "Unfortunately an error occurred: " + error
button.innerText = 'Try again …'
button.disabled = false
console.log(error)
})
})
}, false);
</script>
</body>