-
Notifications
You must be signed in to change notification settings - Fork 0
/
form-handler.js
32 lines (29 loc) · 1.05 KB
/
form-handler.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
// JavaScript (form-handler.js)
document.getElementById('myForm').addEventListener('submit', function(event) {
event.preventDefault();
document.getElementById('myForm').style.display = 'none';
event.preventDefault();
const formData = new FormData(this);
fetch('formhandler.php', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(result => {
try {
const parsedResult = JSON.parse(result);
document.getElementById('status').textContent = `${parsedResult.status}`;
document.getElementById('confirmation').textContent = `${parsedResult.confirmation}`;
document.getElementById('message').textContent = `${parsedResult.message}`;
} catch (e) {
document.getElementById('liveRegion').textContent = `Thank you, your message has been sent:
${result}`;
}
console.log('Success:', result);
})
.catch(error => {
const errorMessage = 'An error occurred. Please try again.';
document.getElementById('status').textContent = errorMessage;
console.error('Error:', error);
});
});