Submit only runs once

Asked

Viewed 92 times

0

$(document).ready(function(){
    //Função que ao clicar no botão, irá fazer.
    $("#logarsist").click(function(){
        var usuario = $("#usera").val();
        var password = $("#pass").val();

        if(usuario == ''){
            $("#formulariousuario").html("Campo Obrigatório*");
            setTimeout(function(){$("#formulariousuario").hide();}, 6000);};

        if(password == ''){
            $("#formulariopassword").html("Campo Obrigatório*");
            setTimeout(function(){$("#formulariopassword").hide();}, 6000);};

        return false;
    });
});
  • I don’t understand your question. Do you want your Submit to run only once or is it only running a single did? If the answer is the first alternative, just change ". click" by ". one" on the third line. If that’s not so, checked if the console displays any error message?

  • it only runs once, no errors on console .

  • actually it runs more than once, only the id with the messages only appear once

  • It doesn’t happen because, after the first time, they’re already Hide(); ?

1 answer

0


From the description you gave me in your question, the code is running, but you forgot to show again the Divs you hid.

I believe that solves your problem:

$(document).ready(function()
{
    $("#logarsist").click(function()
    {
        var usuario = $("#usera").val();
        var password = $("#pass").val();

        if(usuario == '')
        {
            $("#formulariousuario").html("Campo Obrigatório*");
            $("#formulariousuario").show(); // também pode usar o fadeIn()
            setTimeout(function()
            {
                $("#formulariousuario").hide(); // também pode usar o fadeOut()
            }, 6000);
        };

        if(password == '')
        {
            $("#formulariopassword").html("Campo Obrigatório*");
            $("#formulariopassword").show();

            setTimeout(function()
            {
                $("#formulariopassword").hide();
            }, 6000);
        };

        return false;
    });
});

With a little more adaptation in your code you get some better results. See:

$(document).ready(function()
{
    var timeout = null;

    $("#logarsist").click(function()
    {
        var usuario = $("#usera").val();
        var password = $("#pass").val();

        if(usuario == '')
        {
            $("#formulariousuario").html("Campo Obrigatório*").show();
        };

        if(password == '')
        {
            $("#formulariopassword").html("Campo Obrigatório*").show();
        };

        // Cancele o timeout para evitar que ele oculte suas divs em momentos inesperados
        clearTimeout(timeout);
        timeout = setTimeout(function()
        {
            // Acrescente a classe "error" nas divs que deseja exibir as mensagens de erro.
            $('.error').hide();

        }, 6000);

        // Você só deve retornar false se o form tiver erros ou for chamar outra função para realizar o envio do formulário.
        return false;
    });
});

Using a class selector and adding the class to your HTML, you avoid unnecessary code, and can have a more unified treatment.

Jquery also allows you to combine multiple methods into a single selector, reducing the processing of fetching the element again and reducing its code.

  • now they appear and disappear in seconds :/

  • how you would recommend me to send this data to php?

Browser other questions tagged

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