forked from ANSHIKA-26/WordWise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact_us.js
69 lines (59 loc) · 1.99 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
64
65
66
67
68
69
document.addEventListener("DOMContentLoaded", () => {
const sendEmailButton = document.getElementById("sendEmailButton"); // Assuming your button has this ID
sendEmailButton.addEventListener("click", SendEmail);
});
async function SendEmail(event) {
event.preventDefault();
const Name = document.getElementById("Name").value;
const email = document.getElementById("email2").value;
const phone = document.getElementById("phone").value;
const message = document.getElementById("message").value;
console.log(Name,email,phone,message);
if (message.length < 10) {
alert("Message must be at least 10 characters long.");
return;
}
//console.log("Email value:", email , Name, phone, message);
const data = {
Name: Name,
email: email,
phone: phone,
message: message,
};
try {
const response = await fetch("http://127.0.0.1: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.");
}
}