How can I have a button redirect to another page, but still be able to do form validation in HTML?

Catzo _

I'm trying to make a custom order form for this baking website I'm working on. I would like my submit button to redirect to another page once a user has completed the form. I also need the form to be verified too, so all the required information is there.

<div class="flexsetcustomorder">
    <div class="columnleftOrder"> <h2>Custom Order Form</h2> <br>
        <form class="customform" method="GET" target="_self">
          <fieldset>
            <legend>What would you like?</legend>
             <input type="radio" id="cake" name="good" value="cake" required>
             <label for="cake">Cake</label><br>
             <input type="radio" id="cupcake" name="good" value="cupcake">
             <label for="cupcake">Cupcakes</label><br>
             <input type="radio" id="cookie" name="good" value="cookie">
             <label for="cooie">Cookies</label><br><br>
             <textarea id="description" name="describearea" rows="3" cols="50" required> Describe what you'd like here.
             </textarea><br><br>
             <label for="password">Please enter your email:</label><br>
             <input type="email" id="email" name="email" required><br>
          </fieldset>
          <a href="thankyou.html"><button>Submit Order</button> </a>
        </form>
    </div>
Script Host

Here is the HTML code for your custom order form, with the submit button redirecting to another page and the form being verified:

// This function will be called when the user clicks the submit button.
function submitForm() {
  // Check to make sure that the user has entered all of the required information.
  // because of the "required" attributes the following if-condition will never be fulfilled:
  if (document.querySelector("[name=good]:checked").value === "" ||
      document.getElementById("description").value === "" ||
      document.getElementById("email").value === "") {
    alert("Please enter all of the required information.");
    return false;
  }

  // The form is valid, so redirect the user to the thank you page.
  console.log("moving on");
  // window.location.href = "thankyou.html";
}

// Attach the submitForm function to the submit event of the form.
document.querySelector(".customform").addEventListener("submit", submitForm);
<html>
<head>
  <title>Custom Order Form</title>
</head>
<body>
  <div class="flexsetcustomorder">
    <div class="columnleftOrder"> <h2>Custom Order Form</h2> <br>
        <form class="customform" method="POST" action="thankyou.html">
          <fieldset>
            <legend>What would you like?</legend>
             <input type="radio" id="cake" name="good" value="cake" required>
             <label for="cake">Cake</label><br>
             <input type="radio" id="cupcake" name="good" value="cupcake">
             <label for="cupcake">Cupcakes</label><br>
             <input type="radio" id="cookie" name="good" value="cookie">
             <label for="cooie">Cookies</label><br><br>
             <textarea id="description" name="describearea" rows="3" cols="50" required> Describe what you'd like here.
             </textarea><br><br>
             <label for="password">Please enter your email:</label><br>
             <input type="email" id="email" name="email" required><br>
          </fieldset>
          <button type="submit">Submit Order</button>
        </form>
    </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make a button redirect my page to another page?

How do I redirect to a html page and pass variables to that page in Java?

How do I raise PermissionDenied Redirect to another page using CBV?

How to redirect to another HTML page after submitting a form?

How do I redirect to another page on form submit?

How can I make a button redirect my page to another page using a controller action and passing a value using javascript?

How can I on Submit my Form redirect the Results to another Page in React

How can I disable a navbar link after I push a button and successfully redirect to another page?

How can I have form validation using JavaScript?

How can I do form validation with MudBlazor?

How to redirect from one html page to another on button click in javascript?

how could I make a button redirect to another page in play

A button who submits a form and redirect to another page

submit should not redirect to another page in HTML form

HTML/PHP : how can I have multiple form that send informations to the same page?

How can I use validation with form without page refresh?

How to do html5 validation then redirect to another page?

How do I redirect user to another page from clicking a button that's in an iframe

How can I onSubmit redirect the results of an api call to another page?

Clicking on button to redirect to another html page

I am not able to load another html page which I have created as student.html after javascript validation

how do I redirect to another page?

How do I redirect to another page in a condition using php?

How can I duplicate the add to cart button on the single product page, but redirect to another page?

How do I redirect to another page when a user successfully submits a form?

How to redirect to another page with a button?

How to use addEventListener to redirect to another html page on button click?

How i can redirect to another page with data in sveltekit?

How can I redirect a user to another page if they have denied location permission