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

Darwin

I have a HTML page containing a form element. Form contains input of type radio and a button at the end. I want that when button is clicked, a javascript method be called which gets the value of selected choice and redirect to selectedChoice.html page.

Here is my HTML page.

<html>
    <head>
        <script type="application/javascript" src="../js/navigation.js"></script>

    </head>
    <body>
        <div id="chooseBodyPart">
            <form>
                <div class="question-text">
                    <p>Which body part is at dis-ease??</p>
                </div>
                <input class="radio-option" type="radio" name="bodyPartAtDisease" value="chest"> Chest <br>
                <input class="radio-option" type="radio" name="bodyPartAtDisease" value="head"> Head <br>
                <input class="radio-option" type="radio" name="bodyPartAtDisease" value="shoulders"> Shoulders <br>
                <input class="radio-option" type="radio" name="bodyPartAtDisease" value="eyes"> Eyes <br>
                <input class="radio-option" type="radio" name="bodyPartAtDisease" value="ears"> Ears <br>
                <input class="radio-option" type="radio" name="bodyPartAtDisease" value="heart"> Heart <br>
                <button onclick="redirectToNextPageBasedOnResponse();">Proceed</button>
            </form>



        </div>
    </body>
</html>

And below is my js.

function getListOfChoices(form) {
    var choices = form.getElementsByTagName("input");
    return choices;
}


// list of choices should be passed to this method and name of checked choice should be returned.
// In case no choice is selected the return null
function getNameOfCheckedChoice(choices) {
    var selectedChoice = null;
    for (var choiceNumber = 0; choiceNumber < choices.length; choiceNumber++) {
        if (choices[choiceNumber].checked) {
            selectedChoice = choices[choiceNumber].value;
            break;
        }
    }
    return selectedChoice;
}


function redirectToNextPageBasedOnResponse() {
    var divContainingForm = document.getElementById("chooseBodyPart");
    var form = divContainingForm.getElementsByTagName("form")[0];
    var choices = getListOfChoices(form);
    var selectedChoice = getNameOfCheckedChoice(choices);

    window.location.href = "http://" + window.location.host + "/html/" + selectedChoice + ".html";
}

Working of code is exactly as i want but once control returns from redirectToNextPageBasedOnResponse(), URL does not change to what I coded , instead a query param is inserted in the existing URL with value equal to selected choice.

Can someone please explain what is happening??

maazadeeb

You're missing only one thing. Calling preventDefault() on the submit event.

You need to take the event via onclick="redirectToNextPageBasedOnResponse(event);" and then in your handler, do event.preventDefault().

function getListOfChoices(form) {
  var choices = form.getElementsByTagName("input");
  return choices;
}


// list of choices should be passed to this method and name of checked choice should be returned.
// In case no choice is selected the return null
function getNameOfCheckedChoice(choices) {
  var selectedChoice = null;
  for (var choiceNumber = 0; choiceNumber < choices.length; choiceNumber++) {
    if (choices[choiceNumber].checked) {
      selectedChoice = choices[choiceNumber].value;
      break;
    }
  }
  return selectedChoice;
}


function redirectToNextPageBasedOnResponse(event) {
  event.preventDefault();
  var divContainingForm = document.getElementById("chooseBodyPart");
  var form = divContainingForm.getElementsByTagName("form")[0];
  var choices = getListOfChoices(form);
  var selectedChoice = getNameOfCheckedChoice(choices);

  window.location.href = "http://" + window.location.host + "/html/" + selectedChoice + ".html";
}
<div id="chooseBodyPart">
  <form>
    <div class="question-text">
      <p>Which body part is at dis-ease??</p>
    </div>
    <input class="radio-option" type="radio" name="bodyPartAtDisease" value="chest"> Chest <br>
    <input class="radio-option" type="radio" name="bodyPartAtDisease" value="head"> Head <br>
    <input class="radio-option" type="radio" name="bodyPartAtDisease" value="shoulders"> Shoulders <br>
    <input class="radio-option" type="radio" name="bodyPartAtDisease" value="eyes"> Eyes <br>
    <input class="radio-option" type="radio" name="bodyPartAtDisease" value="ears"> Ears <br>
    <input class="radio-option" type="radio" name="bodyPartAtDisease" value="heart"> Heart <br>
    <button onclick="redirectToNextPageBasedOnResponse(event);">Proceed</button>
  </form>
</div>

So, we prevent the default behaviour of the form submit and add our own custom behaviour to it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

redirect to another page after submitting a form

How to redirect after submitting form

How to redirect confirmation page after submitting form in Laravel?

PHPMAILER, how to redirect to the thank you page after submitting the form?

How to redirect to another page after submitting an alert in ASP MVC

How to redirect to another page with passing data after submitting form in using react-router-dom v6?

How to open another submit form after submitting one on html or javascript?

I use java Servlets and cannot redirect to another page after submitting form and sending data in JSON format

How to redirect user after submitting form with React

How to redirect website after submitting google form?

How to redirect after submitting form in php

Contact Form - How to not redirect to another page after submission

How to redirect to another page after form submition using a loading screen

How can I redirect to a new page after submitting a form using Jinja template?

How to redirect to newly created document page after submitting the form firestore for web

How to redirect to List View Page in PHP CodeIgniter using parameters after submitting a form?

How to redirect to a new page using an object id after submitting a form in Django?

"redirect" function of django is not and after submitting the form, user remains on same page

Redirect to contacts page after submitting form in laravel 4

How to redirect on submitting a form?

How to redirect to new html page after form submission in Apps script

Redirect to another page after form submit

Redirect another page after successful form submission

How can i get data or pass a data from one page to another after submitting a form in reactjs

submit should not redirect to another page in HTML form

How do I redirect users after successfully submitting form data?

How to prevent redirect after submitting data through a form

The page is getting reset on submitting form after a while in HTML, javascript

How to redirect to another view/page if the form is invalid

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive