I can’t redirect the page after logging in!

Asked

Viewed 27 times

0

I can’t redirect my page after entering login data windows.location.href command seems not to work!!!

  $(document).ready(function () {
        $("#signup-form").submit(function () {
        var nm1 = $("#name1").val();
        var ps1 = $("#pass1").val();
        localStorage.setItem("n1", nm1);
        localStorage.setItem("p1", ps1);

  });

  $("#login-form").submit(function () {
    var enteredName = $("#name2").val();
    var enteredPass = $("#pass2").val();

    var storedName = localStorage.getItem("n1");
    var storedPass = localStorage.getItem("p1");

    if (enteredName == storedName && enteredPass == storedPass) {
    alert("Voce esta logado!");
     window.location.href = "http://www.devmedia.com.br/";
     
    }
    else {
      alert("Nome do usuario ou palavra passe nao correspondente!");
    }

  });

});

1 answer

3

Hello, you need to use preventDefault(); preventDefault will make your form not submit and you do what you want within the function.

Your Ubmit function would look like this:

//Coloque em sua função o 'event'
$("#login-form").submit(function (event) {
    event.preventDefault();// Adicione essa linha no seu código
    
    var enteredName = $("#name2").val();
    var enteredPass = $("#pass2").val();

    var storedName = localStorage.getItem("n1");
    var storedPass = localStorage.getItem("p1");

    if (enteredName == storedName && enteredPass == storedPass) {
    alert("Voce esta logado!");
     window.location.href = "http://www.devmedia.com.br/";
     
    }
    else {
      alert("Nome do usuario ou palavra passe nao correspondente!");
    }

  });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.