Login with Javascript and PHP - Dando Reload sozinho

Asked

Viewed 740 times

0

I have a PHP login system with JS that is giving me a little problem... When I click login, missing the password, not typing anything or typing, it returns me to a window warning if I missed the password, if I left blank field ous and logged, only that the message appears and disappears in less than 1 second, and already redirects again to index.php

Js

var aviso = {
    inicia: function(titulo, conteudo){
        $('body').css({'margin':'0px', 'padding':'0px'}).prepend('<div id="sombra"></div><div id="alerta"><div id="a_dentro"><div id="topo_alerta">'+titulo+'<div id="close_alerta"><i class="fa fa-times" aria-hidden="true"></i></div></div>'+conteudo+'</div></div>');
        $('#close_alerta').click(function(){
             $('#alerta').fadeOut(200);
             $('#sombra').fadeOut(200);
        });
    }
}
function logar(e){
var usuarioo = $('#usuario').val();
var senhaa = $('#senha').val();
if(usuarioo == '' || senhaa == ''){
aviso.inicia('Esqueceu algo!', 'Digite um usu&aacute;rio e uma senha!');
}else{
$.ajax({
type: "POST",
data: {'usuario':usuarioo, 'senha':senhaa},
url: "library/logar.php",
success: function(html) {
aviso.inicia('Aviso', html);
}
}); 
}
}

PHP

<?php
session_start();
include "configuracoes/config.php";

$usuarioo = $_POST['usuario'];
$senhaa = $_POST['senha'];

$sql_usuario = mysql_query("SELECT * FROM usuarios WHERE usuario='$usuarioo' AND senha='$senhaa' AND status='Ativo'");
$verifica_numero = mysql_num_rows($sql_usuario);
$exibe_usuario = mysql_fetch_array($sql_usuario);

if($verifica_numero > 0){
    session_start();
    $_SESSION['usuario'] = $exibe_usuario['usuario'];
    $_SESSION['nome'] = $exibe_usuario['nome'];
    $_SESSION['id'] = $exibe_usuario['id'];
    $_SESSION['senha'] = $exibe_usuario['senha'];

    $data = date("d/m/Y");
    $hora = date("G:i:s");
    echo'Logado com sucesso!';
    echo'<script>location.reload();</script>';

    $trocar_ip = mysql_query("UPDATE usuarios SET ip_atual='".$_SERVER['REMOTE_ADDR']."' WHERE usuario='".$_SESSION['usuario']."'");

    $insere_log = mysql_query("INSERT INTO logs_acessos (usuario, texto, ip) VALUES ('".$_SESSION['usuario']."', '<b>                           ".$_SESSION['nome']."</b> logou na Intranet em <b>".$data."</b> às <b>".$hora."</b>', '".$_SERVER['REMOTE_ADDR']."')");

}else{
    echo'Usu&aacute;rio ou senha incorreta!';
}
?>

HTML

<?php if(!$_SESSION['usuario']){ ?>
        <form name="login" action="" method="post">
            <input type="hidden" name="acao" value="logar" />
            <label id="titulo_login">Login:</label>
            <input type="text" class="input_login" id="usuario" name="usuario" placeholder="usuário">
            <label id="titulo_login" style="margin-left:15px;">Senha:</label>
            <input type="password" class="input_login" id="senha" name="senha" placeholder="senha">
            <input type="submit" id="log-in" class="logar" href='javascript:;' onclick='logar();' value="Login">
        </form>
        <div id="txt_login">
            Se você ainda não possui conta na Intranet,vá na Sec. Informática para realizar seu cadastro!
        </div>
        <?php }else{?>
        Logado! <div style="float:left;" onClick="sair();">Sair</div>
        <?php } ?>

1 answer

1

Good morning.

Add the following line below Function log(e){, thus:

...
function logar(e){
  e.preventDefault();
...

e. preventDefault() tells the browser not to follow the http request, so it executes its js code and stays on the same page.

  • Brother, gave no, he only gives a Reload on the page and does not give any answer, neither log, nor password error..

Browser other questions tagged

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