我想通过 fetch API 将单击按钮时的表单发送到我验证表单的 index.php。如果表单中没有错误,我想用 PHPMailer 向客户端发送一封电子邮件。由于某种原因,客户没有收到电子邮件。我已经搜索了几个小时来寻找答案,但无法解决问题。
这是 JavaScript 代码:
const form = document.querySelector("#myForm");
form.addEventListener("submit", (e) => {
e.preventDefault();
const formData = new FormData(form);
fetch("index.php", {
method: 'post',
body: formData
}).then((resp) => resp.json())
.then(function (text) {
console.log(text); //Do something with the response, which is an array
if(text !== undefined && text.length > 0) { //The array isn't empty
//Show errors
const formdiverror = document.querySelector(".col-100-form-error");
const colform = document.querySelector(".col-100-form"); //showing error
colform.style.display = "block";
formdiverror.innerHTML = "";
text.forEach(t => formdiverror.innerHTML += t + "</br>");
} else {
//array is empty, no errors
const colform = document.querySelector(".col-100-form");
if(colform !== null || colform !== undefined) colform.style.display = "none";
alert("You succesfully bought the product!");
//window.location.replace("index.html"); //if there was no error redirect to index.html
}
});
})
繁花如伊