-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontactForm-submit.js
109 lines (95 loc) · 3.41 KB
/
contactForm-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
106
107
108
109
$('#fat-btn').click(function () {
// On the submit buttons click event
// Assign the button and the output div to variables
var btn = $(this);
var out = $('#output');
// Initialise the dataString
var dataString = 'submit=submit';
// Get every input in the contactForm
var $inputs = $('#contactForm :input');
// For each input
$inputs.each(function() {
// Throw the name and the value into dataString
dataString = dataString + '&' + this.name + '=' + $(this).val();
});
// Do some wonderful bootstrap magic
btn.button('loading');
// Make a call via ajax to submit.php
// Send data over POST
// Times out after 30 seconds
// Return json
var xhr = $.ajax({
type: 'POST',
url: 'submit.php',
data: dataString,
dataType: 'json',
timeout: 30000,
success: function(response) {
// We have a reply from the submit page,
// it may be good, it may be bad, but we have a reply.
if ( response['status'] == 'error' ) {
// Response is an error, tell the user
//
// Change the buttons class and text
btn.removeClass('btn-primary')
.addClass('btn-danger')
.text('Submit Error');
// Show the error alert
$('#errorDisplay').show().
text(response['errorText']);
// Call the reset function
resetTheForm();
} else {
// Repsonse is not an error
// Change the buttons class and text
btn.removeClass('btn-primary')
.addClass('btn-success')
.text('Success!');
}
// --- DEBUG
// Interpret the json for debugging purposes
$('#myTable').find('tbody').empty();
$.each (response, function (bb) {
$('#myTable').show();
$('#myTable').find('tbody').append(
$('<tr>').append(
$('<td>').text(bb)
).append(
$('<td>').append(
$('<pre>').text(response[bb])
)
)
);
});
// --- END DEBUG
},
error: function(x, t, m) {
// We didn't get a reply from the submit page, did the request time out
if(t==='timeout') {
btn.removeClass('btn-primary')
.addClass('btn-warning')
.text('Req time out');
} else {
btn.removeClass('btn-primary')
.addClass('btn-danger')
.text('Submit Error');
}
resetTheForm();
}
});
})
function resetTheForm() {
// Set a time out
setTimeout(function () {
// Reset the buttons class and text
$('#fat-btn').button('reset')
.removeClass('btn-danger')
.removeClass('btn-warning')
.addClass('btn-primary');
// Hide the alert box
$('#errorDisplay').hide('slow');
// Reload the catpcha
Recaptcha.reload();
// Set the timeout for 7 seconds
}, 7000);
}