JavaScript call within PHP

0

Good afternoon, I'm having trouble running a JavaScript code inside PHP. My intention is for the user to miss the login and password to get a message on the login screen warning him that he missed, this using JavaScript causing him to remove a class called "invisible class".

Follow the code below:

if ($resultado > 0) {
        logaUsuario($usuario->id, $usuario->nome, $usuario->grupo);
        header("Location: index.php");
    } else {
        echo "<script language='javascript' type='text/javascript'>
                function mostrarErro() {
                var alerta = $('.alerta-erro');

                alerta.removeClass('classe-invisivel');
                setTimeout(function() {
                alerta.addClass('classe-invisivel');
                }, 6000);
            }
                mostrarErro();
                window.location.replace('login.php');
              </script>";
    }

I can not see where I'm going wrong. Would anyone guide me, any tips?

Thank you!

    
asked by anonymous 13.03.2018 / 22:42

1 answer

-1

Calling in this way is a bad practice. Put the function in a JS file and escape to the login page in the onload event of the body tag to call the function showErro ().

<?php

// Suas regras de login
$erroLogin = false;

if ($resultado > 0) {
    //Se houver sucesso
    logaUsuario($usuario->id, $usuario->nome, $usuario->grupo);
    header("Location: index.php");

} else {
    //Se falhar
    $erroLogin = true;        
}


[....]
<html>
    <head>
    <script language='javascript' type='text/javascript'>
            function mostrarErro()
            {
                var alerta = $('.alerta-erro');
                alerta.removeClass('classe-invisivel');

                setTimeout(function() {
                    alerta.addClass('classe-invisivel');
                }, 6000);
            }
              </script>
    </head>
    <body <?php echo (isset($erroLogin) && $erroLogin === true) ? 'onload="javascript:mostrarErro();"' : null ;?>>

    [...]

    </body>
</html>
    
13.03.2018 / 22:55