-
Notifications
You must be signed in to change notification settings - Fork 1
/
wpforms-ajax-submit.js
105 lines (93 loc) · 3.01 KB
/
wpforms-ajax-submit.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
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
// WPForms AJAX Submission JS
// this variable will record that a form was submitted through ajax
// or that the form was submitted at all
// this value is provided for a way to test to see if a form was submitted
var wpforms_ajax_submit_done = false;
// this viable will hold the last form id submitted
// another variable that can be tested from another script
var wpforms_last_submitted_form = false;
jQuery(document).ready(function($) {
var trigger = location.hash;
if (!trigger.match(/^#wpforms-/)) {
trigger = false;
}
if (typeof(FormData) == 'undefined') {
if (trigger && trigger.match(/^#wpforms-/)) {
var triggerEl = $($('[data-trigger="'+trigger+'"]')[0]);
var triggerType = triggerEl.attr('data-trigger-type');
if (typeof(triggerType) == 'undefined') {
triggerType = 'click';
}
triggerEl.trigger(triggerType);
wpforms_ajax_submit_done = true;
wpforms_last_submitted_form = trigger;
}
return;
}
$('.wpforms-ajax-submit form').each(function(index, element) {
wpforms_alter_submit(element);
});
});
function wpforms_alter_submit(form) {
// define $
$ = jQuery;
var form_id = $(form).attr('id');
$(form).attr('action', 'javascript: wpforms_ajax_submit("'+form_id+'");');
$(form).append('<input type="hidden" name="action" value="wpforms_ajax_submit" />');
}
function wpforms_ajax_submit(form_id) {
// define $
$ = jQuery;
// disable form
var container = $('#'+form_id).parent();
container.css('position', 'relative');
var overlay = '<div id="'+form_id+'-overlay" style="background-image:url('+wpforms_ajax_submit_data.loading_image+');background-position:left bottom;background-repeat:no-repeat;width:100%;height:100%;position:absolute;background-color:rgba(0,0,0,0.25);z-index:9999;"></div>';
container.prepend(overlay);
// do AJAX request
var ajaxurl = wpforms_ajax_submit_data.ajaxurl;
var form = $('#'+form_id).get(0);
ajaxdata = new FormData(form);
var ajaxobject = {
type: 'post',
dataType: 'json',
url: ajaxurl,
data: ajaxdata,
cache: false,
contentType: false,
processData: false,
success: function(json) {
// enable form
$('#'+form_id+'-overlay').remove();
if (!json) {
return;
}
if (json['redirect_url']) {
window.location = json['redirect_url'];
return;
}
if (json['content']) {
container.empty();
container.append(json['content']);
// make changes to the form that was just inserted
// so it keeps working
// thanks @caleb-smith-capstorm
var form = $('#'+form_id).get(0);
wpforms_alter_submit(form);
}
wpforms_ajax_submit_done = true;
wpforms_last_submitted_form = '#'+form_id;
},
error: function(jqHXR, textStatus, error) {
console.log('error');
console.log(jqHXR);
console.log(textStatus);
console.log(error);
},
complete: function(jqHXR, textStatus) {
//console.log('complete');
//console.log(jqHXR);
//console.log(textStatus);
},
};
$.ajax(ajaxobject);
}