Empty session updating only DIV

Asked

Viewed 325 times

0

I logged in in PHP, where I validated the data via Ajax and I load a PHP page into a DIV, so I don’t have to refresh the whole page. So far so good.

 <?php
 if(!isset($_SESSION)){
    session_start();
 }

 $username = $_POST['name'];
 $password = $_POST['pwd'];
 $username_esc = addslashes($username);
 $password_esc = addslashes($password);

 include("conexao.php");
 mysql_select_db("mg", $conn) or print(mysql_error()); 

 $query = "SELECT id_usuario, nm_login, nm_senha, nm_nome, nm_sobrenome, nm_imagem FROM tbl_usuario WHERE nm_login='".$username_esc."' AND nm_senha='".$password_esc."'";

 $result = mysql_query($query,$conn) or die(mysql_error());
 $num_row = mysql_num_rows($result);
 $row=mysql_fetch_array($result);
 if( $num_row >=1 ) {
   echo 'true';
   $_SESSION['usuario']=$row['id_usuario'];
   $_SESSION['login']=$row['nm_login'];
   $_SESSION['nome']=$row['nm_nome'];
   $_SESSION['sobrenome']=$row['nm_sobrenome'];
   $_SESSION['avatar']=$row['nm_imagem'];
 }
 else{
   echo 'false';
 }
?>

-

    $(document).on('click', '#login', function () {
    username=$("#username").val();
    if (username == "") {
        $("#add_erro_login").html("Digite o usu&aacute;rio cadastrado");
        $("#username").focus();
        return false;
    }

    password=$("#password").val();
    if (password == "") {
        $("#add_erro_login").html("Digite a senha cadastrada");
        $("#password").focus();
        return false;
    }

    $.ajax({
        type: "POST",
        url: "login.php",
        data: "name="+username+"&pwd="+password,
        success: function(html)
        {
            if(html=='true')
            {
                document.form1.loading.style.visibility = "hidden";
                $("#login-form").fadeOut("slow");
                $("#background-on-popup").fadeOut("slow");
                $("#perfil").fadeOut("fast");
                $("#perfil").load("perfil.php");
                $("#perfil").fadeIn("fast");
            }
            else 
            {
                document.form1.loading.style.visibility = "hidden";
                $("#add_erro_login").html("Usu&aacute;rio ou Senha inv&aacute;lido");
            }
        },
        beforeSend:function()
        {
            document.form1.loading.style.visibility = "visible";
            $("#add_erro_login").html("");
        }
    });
    return false;
});

The problem is time to recover the $_SESSION['user'] at the top of the site. Because when it is loaded it is empty, since the user is not logged in, and then as I update only the DIV, it remains empty.

It would have as recovers this variable without having to refresh the whole page?

  • Did you ever solve your previous question? http://answall.com/questions/38590/vari%C3%A1vel-Session-do-php-empty

  • No, that’s why I tried to explain it better

  • Is that if the problem is the same, you should improve that question, and not open a new...

1 answer

0

You have to create a javascrip Rigger to update the div. It would be something like

$( "#foo" ).on( "custom", function( event) {
  // Faz o ajax para ir em um php e retornar os valores da session
});

Then you make the Rigger call at the end of the ajax Success that prepares the login, something like?

$( "#foo").trigger( "custom", [ "Custom", "Event" ] );
  • you know what would be this ajax? I’m researching here, but I haven’t found anything like... vlw

  • Create an ajax like you did for the login, only in the file . php you will send via json the data of the logged in user.

  • I get it, after lunch I’m gonna do it. ...

Browser other questions tagged

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