<a href="PHP/LogOut.php" id="LogOut">Log Out</a>
the link a#LogOut calls a PHP script that ends all sessions and responds with data of “You have logged out”.
<?php
session_start();
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
session_destroy();
echo "You have logged out";
?>
Everything works fine except that once you are logged out and returned to the log in page the ‘data/message’ is not being written to the page. I am suspecting this is due to ‘document.location.href’. If I place a ‘console.log( data )’ it flickers in the console for a split second, then disappears when the return page is loaded. How do I get this to run the script, redirect and display a message?
$('a#LogOut').live('click',function(){
$.ajax({
type: "POST",
url: $(this).attr('href'),
success: function(data){
var message = $(data).text();
if (data==="You have logged out") {
console.log( data );
document.location.href='/returnedpage.php';
$('#messageBox').show().html('<ul></li>' + data + '</li></ul>');
} else {
alert('not working');
}
}
});
return false;
});
I could re-load the content with the .load function, but this has pissed me off and I want to get it right. Basically, I need the ‘message’ to be written to div#messageBox on the ‘returnedpage.php’ page. TY
Sorry if I am not completely understanding your issue – but you console log prior to the redirect, so you see the data from the console.log. Then you are redirected and that data is gone on the subsequent page.
Where is #messageBox located, on the current page OR the redirected page?
If on the current page, you’d probably want to put the redirect after this:
$(‘#messageBox’).show().html(‘
‘ + data + ‘‘);
And add some delay to it so the user can see the message prior to the redirect.
2 If you want to message on the redirected page, then my guess is you’d have to pass data at the end of the url as a param..
And then on the redirected page, grab the name/value pair for “thisData” and then display it.
Apologize if I am totally offbase to your issue.
To do number #2 – on the subsequent page you can just do something like:
and in this page, just do this.