-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.js
52 lines (34 loc) · 1.2 KB
/
actions.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
40
41
42
43
44
45
46
47
48
49
50
51
52
$(document).ready(function () {
const callback = (data, form) => {
form = form[0];
console.log(data.status);
if (data.success) {
alert(data.success);
} else {
if (data.status === 401) { // not valid
form.email.style = 'border-bottom: 1px solid red';
form.name.style = 'border-bottom: 1px solid red';
} else if (data.status === 400) { // isset email in mailchimp
} else if (data.status === 200) { // success
let parent = form.parentElement;
form.querySelector('.answer').insertAdjacentHTML('beforeend', data.message);
// form.remove(); // remove form
}
}
}
const send = (form, path) => {
$.ajax({
type: "POST",
url: path,
dataType: "json",
data: $(form).serialize(),
success: data => callback(data, form)
});
}
// Main
let formSubscribe = document.getElementById('subscribe');
formSubscribe.addEventListener('submit', function (e) {
e.preventDefault();
send($(this), '/wp-admin/admin-ajax.php');
});
});