natal
natal

Reputation: 45

How to check the successful submission of the form and make a redirect?

I have a small form on the page with a name and an email. I want the user to be redirected to another page after submitting the form. Now the data is sent normally with a status of 200. But I can't check for a successful send and then make a redirect. How can i do this? I tried to output the text if(formData.success) to the console, but nothing is output to the console.

const form = document.getElementById("form");
form.addEventListener("submit", formSend);

async function formSend(e) {
  e.preventDefault();
  let formData = new FormData(form);

  let response = await fetch("../sendmail.php", {
      method: "POST",
      body: formData,
    })
    .then((response) => response.json())
    .then((formData) => {
      if (formData.success) {
        console.log(formData);
        window.location.href = url;
      }
    });
}

Upvotes: 0

Views: 594

Answers (1)

akchauhan2.com
akchauhan2.com

Reputation: 209

you will have access to status is formData. You can do it as

if(formData.status === 200){
  console.log(formData);
  window.location.href = url;
}

Upvotes: 1

Related Questions