0

I have a problem with user navigation in a contact form.

I have built (in Bootstrap) a contact form (more precisely a prototype of it, which does not yet need to store data etc), consisting of several pages, currently 3 pages. In this form I have several required fields. Below the form there is a button. Without validation the button looks like this:

<div class="row col-md-1 float-md-right">
<form action="next-page.html">
<button class="btn btn-primary" type="submit">Next</button>
</form>
</div>

And so the button takes me to the next page of the form.

I grabbed the bootstrap template from here for validation: https://getbootstrap.com/docs/4.6/components/forms/#validation and copied it into my main.js:

(function() {
  'use strict';
  window.addEventListener('load', function() {
    // Fetch all the forms we want to apply custom Bootstrap validation styles to
    var forms = document.getElementsByClassName('needs-validation');
    // Loop over them and prevent submission
    var validation = Array.prototype.filter.call(forms, function(form) {
      form.addEventListener('submit', function(event) {
        if (form.checkValidity() === false) {
          event.preventDefault();
          event.stopPropagation();
        }
        form.classList.add('was-validated');
      }, false);
    });
  }, false);
})();

Validation works with this JS snippet, but I don't get to the next page. How do I do this now?

I (regretfully) didn't use any frameworks like Angular, there would be the possibility of routing. But how do I get it to work like this now?

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
  • That js snippet is expecting your forms to have a `needs-validation` class. Is that the case? – lbsn Apr 30 '21 at 13:56
  • Yes, I have the following class in my HTML pages:
    – wupperdouble Apr 30 '21 at 13:58
  • So what is not working exactly? What do you mean by "I don't get to the next page"? Are submitting a valid form and still not navigating away? – lbsn Apr 30 '21 at 14:00
  • Correct. I send a valid form, but I don't get the next page of my contact form. – wupperdouble Apr 30 '21 at 14:03
  • It's hard to tell without seeing your code. It should work, as long as your submit callback gets executed and your form have the `action` attribute. [https://stackblitz.com/edit/js-2zps42](https://stackblitz.com/edit/js-2zps42) – lbsn Apr 30 '21 at 14:40
  • Okay, I understood the example in your link, thank you very much. If you could tell me how to open another page in Javascript, I would be able to build a quick and dirty solution. – wupperdouble Apr 30 '21 at 19:07
  • You can take a look at this [answer](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) to know how to navigate to an url using JS. – lbsn May 03 '21 at 07:36
  • None of the solutions there work. window.location.href doesn't, window.location.replace doesn't either, both functions without window in front of it don't either. I don't know what to do. – wupperdouble May 03 '21 at 10:33
  • Please share your code! We can't just guess what your trying to do and how and why it's not working. [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – lbsn May 03 '21 at 10:55

0 Answers0