Submit login and password!

2

I have a simple form to login for users with login and password ... I'm not able to give submit via JQuery, look at the code:

<div id="opcoes2">
<div id="fazerlogin2">
<form name="fazerloginform" id="fazerloginform" action="fazerloginform.php" method="post">
<input id="login" type="text" name="login" value="Login ou email">
<input id="senha" type="password" name="senha" value="Senha">
<span id="esqueceusenha">Esqueceu a senha ?</span>
<input id="entrar" type="submit" value="ENTRAR">
<div id="loginresposta"></div>
</form>
</div>

<script>  
$(document).ready(function() {
        $("#fazerloginform").submit(function() {
            var login = $("#login").val();
            var senha = $("#senha").val();
            $("#entrar").prop('disabled', true);
            $("#loginresposta").html("Logando...");
            $.post('fazerloginform.php', {
                login: login,
                senha: senha
            }, function(resposta) {
                if (resposta != false) {
                    $("#fazerloginform").submit();
                } else {
                    $("#loginresposta").html("Login ou senha incorretos.");
                    $("#entrar").prop('disabled', false);
                }
            }, 'html');
            return false;
        });

 })
</script>  

I'm using jquery version 2.1.1.js, is this it? The error is that it is "Logging ..." and for the script, do not give the submit !!!

I also wanted to know the best way to login in php and mysql with jquery?

    
asked by anonymous 10.08.2014 / 21:28

3 answers

1

I see two problems here. One is that you still have the active event handler when you want to submit it in the script.

The event handler $("#fazerloginform").submit(function() { is being called when you $("#fazerloginform").submit(); within success. Since the processing is asynchronous the code goes straight to return false; and the submi is canceled, as if it had a evento.preventDefault(); . So you need to remove the event handler to make submit without being intercepted.

Test like this:

$(document).ready(function() {
    $("#fazerloginform").on('submit', function() { // usei .on() aqui, pois uso .off() embaixo
        var self = this;
        var login = $("#login").val();
        var senha = $("#senha").val();
        $("#entrar").prop('disabled', true);
        $("#loginresposta").html("Logando...");
        $.post('fazerloginform.php', {
            login: login,
            senha: senha
        }, function(resposta) {
            if (resposta != false) {
                $(self).off("submit"); // aqui remove o event handler
                $(self).submit(); // agora já pode fazer submit sem chamar este código novamente
            } else {
                $("#loginresposta").html("Login ou senha incorretos.");
                $("#entrar").prop('disabled', false);
            }
        }, 'html');
        return false;
    });

 });
    
11.08.2014 / 07:37
1

I was able to change the submit for a click on the submit button on form ...:

<script>
$(document).ready(function() {
        $("#entrar").click(function() {
            var login = $("#login").val();
            var senha = $("#senha").val();
            $("#entrar").prop('disabled', true);
            $("#respostalogina").html("Logando...");
            $.post('fazerloginform.php', {
                login: login,
                senha: senha
            }, function(resposta) {
                if (resposta == true) {
                    $("#fazerloginform").submit();
                } else {
                    $("#respostalogina").html("Login ou senha incorretos.");
                    $("#entrar").prop('disabled', false);
                }
            }, 'html');
            return false;
        });

})
</script>  

I'm still waiting for answers.s ...

    
10.08.2014 / 22:25
1

You do not need to use $("#fazerloginform").submit() if the answer is true. See if this example helps ...

$('form[name="NOMEDOFORM"]').submit( function()
{
    event.preventDefault();
    $('#respostalogina').html( 'logando...' );

    //
    $.ajax({
        url      : 'fazerloginform.php',
        type     : 'POST',
        data     : { login : $("#login").val() , senha : $("#senha").val() },
        success  : function( dataCheck )
        {
            if( dataCheck == false )
            {
                $("#respostalogina").html( 'Login ou senha incorretos.' )
            }
            else
            {
                // login feito
            }
        }
    });
});
    
10.08.2014 / 23:45