-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.js
More file actions
38 lines (35 loc) · 1.41 KB
/
Copy pathcontact.js
File metadata and controls
38 lines (35 loc) · 1.41 KB
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
document.getElementById('contactForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the form from submitting the traditional way
// Get form values
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const message = document.getElementById('message').value;
// Prepare the data to be sent
const formData = {
name: name,
email: email,
message: message
};
// Send data to the server (replace with your server's URL)
fetch('http://localhost:4001/contact', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData) // Convert form data to JSON
})
.then(response => response.json()) // Parse the response as JSON
.then(data => {
// Show success or error message based on the response
if (data.success) {
document.getElementById('status').textContent = 'Thank you for your message!';
} else {
document.getElementById('status').textContent = 'Something went wrong, please try again.';
}
})
.catch(error => {
// Handle any errors that occur during the fetch request
console.error('Error:', error);
document.getElementById('status').textContent = 'An error occurred, please try again later.';
});
});