Problem with window.Location

Asked

Viewed 78 times

1

I’m having trouble with window.location = "index.php", he is not redirecting the page login.php for index.php, see the code:

<script type="text/javascript">
  $(function() {
    $("#loginForm").on("submit", function(a) {
      a.preventDefault(), $("#signinButton").attr("value", "Autenticando...");

      var user = $("#username").val();
      var pass = $("#password").val();

      if (user == "") {
        $("#message").css("display", "block"), $('#message').html("<div class='alert alert-danger'><i class='fa fa-warning'></i> Insira seu usuário do Twitter</div>"), $("#signinButton").attr("value", "Entrar");
        return false;
      }

      else if (pass == "") {
        $("#message").css("display", "block"), $('#message').html("<div class='alert alert-danger'><i class='fa fa-warning'></i> Insira sua senha do Twitter</div>"), $("#signinButton").attr("value", "Entrar");
        return false;
      } else {
        $("#message").hide();
      }

      var b = $("#username").val();

      0 == /^[a-zA-Z0-9_ ]*$/.test(b) ? ($("#message").css("display", "block"), $("#message").html("<div class='alert alert-danger'><i class='fa fa-warning'></i> Existem caracteres especiais no seu usuário. Se estiver usando <strong>@</strong> remova-o!</div>"), $("#signinButton").attr("value", "Entrar")) : $.ajax({
        type: "POST",
        url: "api/login.php",
        dataType: "JSON",
        data: $("#loginForm").serialize(),
        success: function(a) {
          1 == a.redirect ? window.location = "index.php" : ($("#message").css("display", "block"), $("#message").html(a.message)), $("#signinButton").attr("value", "Entrar");
        }
      })
    })
  })
</script>

Here is my answer code is much bigger than this but I will post only the check:

if (isset($cookies['auth_token'])) {
    Cookies::set('auth_token',            $cookies['auth_token']);
  Cookies::set('password',              $ttrPassword);

    $_SESSION[SITE_NAME . '_session'] = $ttrUsername;

    echo json_encode(array('status' => 'success','message'=> "<div class='alert alert-success'><i class='fa fa-check'></i> Autenticação bem sucedida, estamos te redirecionando</div>"));
} else {
    echo json_encode(array('status' => 'error','message'=> "<div class='alert alert-danger'><i class='fa fa-warning'></i> Não foi possível autenticar com o Twitter.</div>"));
}

What problem, is in return json, or my javascript is incorrect?

  • I’ve seen it, and it has nothing to do with my problem

  • same thing... does not redirect...

  • What do you mean? I don’t really understand.

  • 2

    Wow, dude vlw poe as answer q marco as solved

1 answer

2


In your PHP you forgot to set the position redirect that you make the comparison on JS.

1 == a.redirect ? window.location = "index.php" : ....

Then just set a value for it in your PHP that will work:

 echo json_encode(
         array('status' => 'success',
                'message'=> "<div class='alert alert-success'><i class='fa fa-check'></i> Autenticação bem sucedida, estamos te redirecionando</div>",
                "redirect"=>$algum_valor));
  • worked but now stopped again I do not know why

  • You have tried using window.location.href = ' x';

Browser other questions tagged

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