progodyssey/public/index.js

41 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-08-31 13:06:03 +00:00
document.getElementById('applicationForm').addEventListener('submit', function(event) {
event.preventDefault();
2024-08-31 13:06:03 +00:00
// Get form data
const formData = new FormData(this);
const data = Object.fromEntries(formData.entries());
// Clear previous messages
const responseMessage = document.getElementById('responseMessage');
responseMessage.textContent = '';
responseMessage.className = '';
// Send form data to server-side endpoint
fetch('/submit', {
2024-08-31 13:06:03 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => {
if (!response.ok) {
// If the response status is not OK, throw an error
return response.json().then(err => {
throw new Error(err.message || 'An error occurred');
});
}
return response.json();
})
2024-08-31 13:06:03 +00:00
.then(result => {
console.log('Success:', result);
responseMessage.textContent = 'Thank you for your application!';
responseMessage.className = 'success';
2024-08-31 13:06:03 +00:00
})
.catch(error => {
console.error('Error:', error);
responseMessage.textContent = `Error: ${error.message}`;
responseMessage.className = 'error';
2024-08-31 13:06:03 +00:00
});
});