-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact.js
35 lines (32 loc) · 1019 Bytes
/
contact.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
// contact.js
document.getElementById('contact-form').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent default form submission
// Get form data
const formData = new FormData(this);
// Convert form data to JSON
const jsonData = {};
formData.forEach((value, key) => {
jsonData[key] = value;
});
// Send form data to server using fetch API
fetch('/submit-form', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
})
.then(response => {
if (response.ok) {
alert('Message sent successfully!');
// Optionally, reset the form
this.reset();
} else {
alert('Failed to send message. Please try again later.');
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while sending the message. Please try again later.');
});
});