dmxsta
dmxsta

Reputation: 11

Redirect to another page after 10 seconds using javascript and a form submit

So I have a very basic form submit which redirects to another page after the user enters there username and presses submit.

<form action="https://example.com/" method="GET">
 <input type="text" name="username" required> <br>
<input type="submit">
</form>

The URL will then display

https://example.com/?username=your-entered-username

However, I want to try and add a redirect timer on this page so after the user presses submit they have to wait 10 seconds then it redirects them to:

https://example.com/?username=your-entered-username

I would also like to add in a countdown timer saying something like "Please wait x seconds"

I managed to get it to redirect after 10 seconds but it doesn't grab the ?username= part. I feel like I have tried everything I possibly can so asking for help on here is my last resort. I am still trying to learn the basics of coding and I would really appreciate it someone could help me with this.

Thanks!

Upvotes: 0

Views: 1455

Answers (1)

ThS
ThS

Reputation: 4783

You can achieve this by the help of JavaScript.

So how will we make this work :

  • intercept the submission of the form, add a submit listener to it in other words.
  • based on a counter (a variable) we'll wait for about 10 seconds.
  • get the action attribute of the form append the input value to it.
  • redirect based on the link composed by the action attribute and the input's value.

Here's a demo, it contains some helpful clarifications :

/** waiting until the document is loaded **/
document.addEventListener("DOMContentLoaded", () => {
  /** selecting necessary elements **/
  const form = document.getElementById("my-form"),
    username = form.querySelector('input[name="username"]'),
    countdown = document.getElementById("countdown"),
    remSec = countdown.querySelector("span.count");
  let countSec = 10; /** seconds to wait **/

  /** adding "submit" listener to the "form" **/
  form.addEventListener("submit", e => {
    /** it's better to implement a validation for the input's value but for the demo purposes we'll proceed without no any validations **/
    e.preventDefault(); /** preventing the foem from submitting so we can do it programatically after the 10 seconds **/
    /** show the countdown **/
    countdown.classList.add('visible');
    /** the function is executed every second. It mainly updates how many seconds left and if the 10 seconds are spent it redirects **/
    timer = setInterval(() => {
      if (countSec >= 0) remSec.textContent = countSec--;
      else {
        window.location.href = form.action.replace(/\/$/, "") + "/?username=" + username.value; /** the link is formed using the "target" attribute and the input's value **/
        /** .replace(/\/$/, "") : trims the "/" char at the end of the target attribute if exists because we do add it manually so we ensure that it is always there **/
      }
    }, 1000); /** every second **/
  });
});
.overlay {
  display: none;
  /** the countdown is hidden initially **/
  position: fixed;
  /** stays in place even if scrolling happens **/
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  /** the next two rules needs "display: flex" rule to take effect. Note now the "display" property is set to "none" and later it will be changed to "flex" via "JavaScript" **/
  justify-content: center;
  /** centered horizontally **/
  align-items: center;
  /** centered vertically **/
  background-color: rgba(24, 24, 24, .6);
  z-index: 999;
  /** stays on top **/
}


/** style for the text in the countdown "div" **/
.overlay>p {
  padding: 15px;
  background-color: #f5f5f5;
  text-align: center;
  font-size: 1.5rem;
  border-radius: 6px;
  box-shadow: 0 0 25px -2px rgba(18, 18, 18, .75);
}


/** styling the count down seconds numbers (seconds left) **/
.overlay>p .count {
  display: inline-block;
  vertical-align: middle;
  width: 35px;
  height: 35px;
  background-color: #000;
  color: #fff;
  line-height: 35px;
  text-align: center;
  border-radius: 50%;
}


/** this class is used to change the "display" to "flex" of the countdown "div". It is used by "JavaScript" **/
.overlay.visible {
  display: flex;
}
<!-- added an "id" to the form element to easily select it with "JavaScript" -->
<form id="my-form" action="https://example.com/" method="GET">
  <input type="text" name="username" required> <br>
  <input type="submit" value="go">
</form>

<!-- the "div#countdown" will have the count down till redirection happens, also I added some styling to it to be more realistic in this example -->
<div id="countdown" class="overlay">
  <p>You have to wait <span class="count">10</span> seconds before redirecting...</p>
</div>

Some helpful links :

Learn more about flexbox.

Learn more about addEventListener function.

Learn more about setInterval function.

Learn more about replace function.

Hope I pushed you further.

Upvotes: 2

Related Questions