How do I go to the thank you page after a successful submission?
-
There is such a code in pure JS.
Tell me what needs to be removed here or what should be added so that after a successful submission I will be redirected to the thank you page?
form.addEventListener("submit", (e) => { e.preventDefault(); const phoneInput = document.getElementById("quiz-phone"); const emailInput = document.getElementById("quiz-email"); answersObj.step4.phone = phoneInput.value; answersObj.step4.email = emailInput.value; if (phoneInput.value === "" || emailInput.value === "") { alert("Введите данные формы"); } else if(emailInput.classList.contains('error')) { alert("Введите верный email"); } else if(phoneInput.classList.contains('error')) { alert("Введите верный номер телефона"); } else { postData(answersObj) .then((res) => res.json()) .then((res) => { if (res.result === "success") { window.location = "/thanks.html"; form.reset(); } else { alert(res.status); } }); } }); const postData = (body) => { return fetch("./quiz-send.php", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(body), }); };
The send.php file containsheader ('Location: thanks.html');
At the moment, the form is being sent, but as already understood, it does not translate to the thanks page :(JavaScript Nicholas Rangel, May 14, 2020 -
Most likely, the page does not go to the page because the
fetch
request ends with an error.
Catch the error usingcatch
postData(answersObj)
.then((res) => res.json())
.then((res) => {
if (res.result === "success") {
window.location = "/thanks.html";
form.reset();
} else {
alert(res.status);
}
})
.catch(err=>alert(err.message)); // ошибка отправки формыClaire Ponce
1 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!