0

So im trying to figure out whats wrong here!

Im using Ajax to log users in and out and refresh some part of the document as they do. Everything is working fine but when i log out i have to click my link twice in order to see the changes.

It look like im destroying the session after all the instruction have been completed.

Here is how it work (or almost!)

The document html/php

   <div id="top">  
<?php 
$top = new top();
$top->display();
?>
</div>

So im just calling a class that make the content of the top of the document and this is working fine.

In the top when your logged in you have the logout option and the following jquery/ajax call

 $("#unlog").live("click",function(){
     req_content("top","user","logout","goodbye");
       });

So when i click logout i run req_content() function and this is all working well

Now when i click it y run my kinda ajax_query responder

if($ajax_action=='logout'){

     unset($_SESSION['user']);
     session_destroy();

require('class.php');

$top=new top();
$top->display();

 }

Now what happend is that i run the query but the page kinda refresh before the session var is actualy destroyed. If i click it a second time it is working as expected becuse the session is already destroyed.

Anyone can explain this? The process is the same with my login and it is working fine.

The req_content() function

function req_content(box,user,action,query) {

    var xhr = getXMLHttpRequest();
    document.getElementById(box).innerHTML="<img src='img/load.gif' alt'Loading....'>";
    xhr.onreadystatechange = function() {

        if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
            document.getElementById(box).innerHTML=xhr.responseText;
        }





    };

    var sVar1 = encodeURIComponent(box);     /* Endroit ou on demande le contenu*/
    var sVar2 = encodeURIComponent(user);    /* Qui demande le contenu(facultatif)*/
    var sVar3 = encodeURIComponent(action);  /* Quel action veut-on effectuer*/
    var sVar4 = encodeURIComponent(query);   /* La requete pour efectuer l'action*/

    xhr.open("GET", "ajax_http.php?box=" + sVar1 + "&user=" + sVar2 + "&action=" + sVar3 +"&query=" + sVar4, true);
    xhr.send(null);

}

2 Answers 2

1

Your description is that you are clicking a link. Chances are the page is refreshing before the AJAX is complete.

Try:

$("#unlog").live("click", function() {
    req_content("top", "user", "logout", "goodbye");
    /* prevent browser default event on element such as link being followed, or form being submmitted*/
    return false;
});

Instead of sending back the normal content you can also return from your php:

 print_r( $_SESSION);

As response for AJAX and see what gets sent back. It will get put into your page where the expected content would go.

Sign up to request clarification or add additional context in comments.

2 Comments

OMG i cant beleive i did that! print_r($_SESSION) brought it up to me i was actualy working with a admin session not a user session. Thanks a ton.
a lot of people think AJAX is voodoo between server and browser because can't see what server is doing sometimes. Simple tricks like this can save huge amounts of time and pain...glad it helped
1

Try to put a redirect in this script:

    if($ajax_action=='logout'){

         unset($_SESSION['user']);
         session_destroy();

/* HERE INSERT A REDIRECT like header("location: someurl");*/

    require('class.php');

    $top=new top();
    $top->display();

     }

4 Comments

Well if for exemple i set the location to that same page im getting the entire page in the top. However the session have been destroyed and i can see the login option again. So it kinda make the ajax useless right now cuse im reloading a full page in hte top so do i have to make a page that will fit in the top with only what i want there? It seams like im requesting a request!?
Is this just a mather of refreshing?
How you check if the session USER isset?
im not using jquery for my ajax only for animation and event management. I use a basic javascript xmlhttprequest for my ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.