Cristian Lehuede Lyon
Cristian Lehuede Lyon

Reputation: 1947

JavaScript redirect in modal

I've been struggling with this for a while but I can't seem to figure this out. I've got a modal that displays and asks for user login and password. Right now if user ==admin it should redirect to Google but for some reason it doesn't work.

Any indications on why is this happening?

<form id="loginform" name="loginform" method="post" action="index.html">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" class="txtfield" tabindex="1">
    <label for="password">Password:</label>
    <input type="password" name="password" id="password" class="txtfield" tabindex="2">
    <div class="center">
        <input type="submit" name="loginbtn" id="loginbtn" class="flatbtn-blu hidemodal" value="Log In" tabindex="3">
    </div>
</form>
</div>
<script type="text/javascript">
    $(function() {
        $('#loginform').submit(function(e) {

        });

        $('#modaltrigger').leanModal({
            top: 110,
            overlay: 0.45,
            closeButton: ".hidemodal"
        });
    });

    $('#loginbtn').click(function() {
        var username = $("#username").val();
        var password = $("#password").val();
        if (username == "admin") {
            window.location.href = "http://kegan.ing.puc.cl/~grupo6/faseiv/index.php";
        } else {
            alert("Usuario o contraseña invalido");
        }
    });

Upvotes: 0

Views: 2151

Answers (1)

scrowler
scrowler

Reputation: 24405

OK, looking at it first, your $('#loginbtn') code is outside your DOM ready scope. Secondly, I'd remove that selector completely and bind your code to the form's submit event - it's much more accurate than the submit buttons click.

$(function() {
    $('#loginform').submit(function(e) {
        e.preventDefault();
        var username = $("#username").val();
        var password = $("#password").val();
        if (username == "admin") {
            window.location.href = "http://kegan.ing.puc.cl/~grupo6/faseiv/index.php";
        } else {
            alert("Usuario o contraseña invalido");
        }
    });
});

...if you were to continue to use the click handler on your submit button, you need to stop #loginform from submitting normally.

Upvotes: 1

Related Questions