-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (39 loc) · 1.62 KB
/
Copy pathscript.js
File metadata and controls
46 lines (39 loc) · 1.62 KB
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
const API_BASE = "http://localhost:5000";
const form = document.getElementById("contactForm");
const msgEl = document.getElementById("formMessage");
const emailRx = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
function setMsg(text, type) {
msgEl.textContent = text;
msgEl.className = "msg " + (type || "");
}
form.addEventListener("submit", async (e) => {
e.preventDefault();
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
const subject = document.getElementById("subject").value.trim();
const message = document.getElementById("message").value.trim();
if (!name || !email || !subject || !message) {
return setMsg("⚠️ All fields are required.", "error");
}
if (!emailRx.test(email)) {
return setMsg("⚠️ Please enter a valid email address.", "error");
}
setMsg("Sending…");
try {
const res = await fetch(`${API_BASE}/api/contact`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, email, subject, message })
});
if (res.ok) {
setMsg("✅ Your message has been sent!", "success");
form.reset();
} else {
const data = await res.json().catch(() => ({}));
setMsg(`❌ ${data.error || "Something went wrong."}`, "error");
}
} catch (err) {
console.error(err);
setMsg("❌ Unable to reach server. Check API_BASE and CORS.", "error");
}
});