forked from ANSHIKA-26/WordWise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact_us.js
63 lines (55 loc) · 1.78 KB
/
contact_us.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
async function SendEmail(event) {
event.preventDefault();
const firstName = document.getElementById("firstName").value;
const lastName = document.getElementById("lastName").value;
const email = document.getElementById("email").value;
const phone = document.getElementById("phone").value;
const message = document.getElementById("message").value;
if (message.length < 10) {
alert("Message must be at least 10 characters long.");
return;
}
const data = {
firstName: firstName,
lastName: lastName,
email: email,
phone: phone,
message: message,
};
try {
const response = await fetch("http://localhost:3000/send-email", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const result = await response.json();
if (response.ok) {
const form = document.getElementById("contactForm");
const popup = document.getElementById("popupMessage");
const overlay = document.getElementById("overlay");
const closePopup = document.getElementById("closePopup");
// Show popup
overlay.style.display = "block";
popup.style.display = "block";
// Clear the form
form.reset();
// Close the popup
closePopup.addEventListener("click", function () {
popup.style.display = "none";
overlay.style.display = "none";
});
// Close the popup when clicking outside of it
overlay.addEventListener("click", function () {
popup.style.display = "none";
overlay.style.display = "none";
});
} else {
alert(`Error: ${result.message}`);
}
} catch (error) {
console.error("Error sending email:", error);
alert("Error sending email. Please try again later.");
}
}