You should execute an ajax
call with the help of jQuery
.
We can use the shorthand method .post
to issue a post request directly.
First put the php
code in a separate file, called logout.php
logout.php
function runMyFunction(){
$_SESSION["identificate"] = 'no';
session_destroy();
//header('Location: Login.html'); we will move the redirect to the success handler of the ajax call
}
//call the function
runMyFunction();
Then in the html
, include jquery
and add a script
block like below
<script type="text/javascript">
$(function() {
$("#foo button").click(function(){
$.post( "logout.php", function( data ) {
console.log("success");
window.location = "login.html"; // redirect moved here, after the logout happened successfully
});
})
});
</script>
You can remove the onclick
attribute from the button, so your button
html will become this
<button type="button">Logout</button>
This is how you include jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>