-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
45 lines (37 loc) · 1.51 KB
/
index.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
// Function to show the modal
function showModal(message) {
var modal = document.getElementById("myModal");
var modalMessage = document.getElementById("modalMessage");
// Set the message in the modal
modalMessage.textContent = message;
// Show the modal
modal.style.display = "block";
// Add event listener to close the modal when the close button is clicked
var closeButton = document.getElementsByClassName("close")[0];
closeButton.addEventListener("click", function() {
modal.style.display = "none";
});
}
// Add event listener to the form submission
document.getElementById("myForm").addEventListener("submit", function(event) {
event.preventDefault(); // Prevent the default form submission
// Show the modal immediately to indicate form submission is in progress
showModal("Submitting form...");
// Perform an AJAX request to submit the form
var xhr = new XMLHttpRequest();
xhr.open("POST", this.action);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
// Successful response
var response = xhr.responseText;
showModal(response); // Show the modal with the response message
document.getElementById("myForm").reset(); //Clear the form fields
} else {
// Error response
showModal("Error: Something went wrong."); // Show a generic error message
}
}
};
xhr.send(new FormData(this));
});