You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB
JavaScript

const apiMail = "https://www.pychr.com/pychr/email/sendEmail";
const data = {
// to: "k.y.kingsonyu@gmail.com",
subject: "",
body: "",
};
function sendFormData() {
const author = document.getElementById("author").value;
const phone = document.getElementById("phone").value;
const email = document.getElementById("email").value;
const select = document.querySelector(".select-wrap select");
const selectedValue = select.options[select.selectedIndex].textContent;
const comment = document.getElementById("comment-message").value;
const combinedData = `${author}\n${phone}\n${email}\n${selectedValue}\n${comment}`;
data.subject = selectedValue;
data.body = combinedData;
fetch(apiMail, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to send message");
}
})
.then((data) => {
console.log("Message sent successfully:", data);
alert("Message sent successfully");
})
.catch((error) => {
console.error("Error sending message:", error);
});
}
document
.getElementById("commentform")
.addEventListener("submit", function (event) {
event.preventDefault(); // 阻止表单默认提交行为
sendFormData();
});