/
leads_form.js
39 lines (38 loc) · 1.67 KB
/
leads_form.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
//selector from your HTML form
$('#leads-form').submit(function (e) {
//prevent the form from submiting so we can post to the google form
e.preventDefault();
//AJAX request
$.ajax({
url: 'https://docs.google.com/forms/d/e/1FAIpQLSe1lOKSU4NGTqy2KEhbxFu2MM4GLKfTm5tCYbmoLVLyo6Imlw/formResponse', //The public Google Form url, but replace /view with /formResponse
data: $('#leads-form').serialize(), //Nifty jquery function that gets all the input data
type: 'POST', //tells ajax to post the data to the url
dataType: "json", //the standard data type for most ajax requests
statusCode: { //the status code from the POST request
0: function (data) { //0 is when Google gives a CORS error, don't worry it went through
console.log('form submitted');
//success
$('#form_success').text('Welcome!');
},
200: function (data) {//200 is a success code. it went through!
console.log('form submitted');
//success
$('#form_success').text('Welcome!');
},
403: function (data) {//403 is when something went wrong and the submission didn't go through
console.error('problem submitting form')
//error
alert('Oh no! something went wrong. we should check our code to make sure everything matches with Google');
},
404: function (data) { //404 is when Google gives a CORS error too?, don't worry it went through
console.log('form submitted');
//success
$('#form_success').text('Welcome!');
},
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.debug("error in form");
console.log(XMLHttpRequest);
}
});
});