-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
81 lines (56 loc) · 1.6 KB
/
scripts.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
const submitButton = document.getElementById("form-submit");
const form = document.getElementsByTagName ("form");
const successMessage = document.getElementById("success-message");
const updateState = (message)=>{
submitButton.innerHTML = message;
};
const handleSuccess = ()=> {
updateState("Sucess!");
setTimeout(()=> {
//hide the form
form [0].style.display = "none";
//display sucess message
successMessage.style.display = "block";
}, 1000 );
};
const handleError =()=>{
updateState("something went wrong!");
setTimeout(() => {
updateState("Schedule a Call")
}, 1500)
};
const submitForm = (event) => {
//prevent form from refreshing
event.preventDefault();
//updating the state
updateState("submitting...");
//process the form data
const formData = new FormData (event.target);
const formObject = {};
formData.forEach ((value,key) => {
formObject [key] = value;
});
console.log(formObject);
//create jason string
const jsonString = JSON.stringify(formObject);
console.log(jsonString);
//submit to formspark
fetch("https://submit-form.com/PTTuGlWSt", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: jsonString,
})
.then(function (response) {
if(response.ok){
handleSuccess();
}
console.log(response);
})
.catch(function (error) {
console.error(error);
handleError();
});
};