-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformSubmit.js
59 lines (53 loc) · 1.88 KB
/
formSubmit.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
document.addEventListener('DOMContentLoaded', function() {
const form = document.querySelector('.join-form');
const phoneInput = document.getElementById('phone');
const submitBtn = document.querySelector('.submit-btn');
const arrow = submitBtn.querySelector('.arrow');
const spinner = submitBtn.querySelector('.btn-spinner');
const checkmark = submitBtn.querySelector('.checkmark');
function setButtonState(state) {
arrow.style.display = state === 'arrow' ? 'inline' : 'none';
spinner.style.display = state === 'spinner' ? 'inline' : 'none';
checkmark.style.display = state === 'checkmark' ? 'inline' : 'none';
submitBtn.disabled = state === 'spinner';
}
form.addEventListener('submit', function(e) {
e.preventDefault();
const phoneNumber = phoneInput.value.trim();
if (!/^\d{10}$/.test(phoneNumber)) {
alert('Please enter a valid 10-digit phone number.');
return;
}
setButtonState('spinner');
// Create an object with form data
const formData = Object.fromEntries(new FormData(form));
fetch(form.action, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(formData)
})
.then(response => {
console.log('Response:', response);
if (response.ok) {
setButtonState('checkmark');
form.reset();
console.log('Form submitted successfully');
alert('Thank you for your submission! Someone from our team will reach out to you soon for the next steps.');
} else {
throw new Error('Form submission failed');
}
})
.catch(error => {
console.error('Error:', error);
alert('There was an error submitting the form. Please try again.');
})
.finally(() => {
setTimeout(() => {
setButtonState('arrow');
}, 2000);
});
});
});