I have a function that will post a value to a php page and then run some sql. after it is done I am trying to set the window location.
It works perfectly if I don not have window.location.href, however when I add this line of code It changes the page but does not do the rest.
$(document).ready(function ()
{
$('.delete').click(function ()
{
$('.confirm').toggleClass('confirmShow');
var clickBtnValue = $(this).val();
var ajaxurl = 'ajaxDelete.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
});
if ($(this).val() == "Delete Account")
{
$(this).val("yes");
}
else if ($(this).val() == "yes")
{
$(this).val("Delete Account");
//When the below line is removed it works perfectly
window.location.href = 'Functions/LogOut.php';
}
else if ($(this).val() == "No")
{
$('#delete').val("Delete Account");
}
});
});
As soon as you click the button, you redirect to the Logout page. The ajax call doesn't have time to execute. Put the redirect in the callback (which is currently empty)
$('.delete').click(function () {
$('.confirm').toggleClass('confirmShow');
var clickBtnValue = $(this).val();
var ajaxurl = 'ajaxDelete.php',
data = { 'action': clickBtnValue };
$.post(ajaxurl, data, function (response) { // <-- This function is the callback. It will be executed after the ajax call is done
if ( clickBtnValue == "Delete Account") {
$(this).val("yes");
} else if (clickBtnValue == "yes") {
$(this).val("Delete Account"); // Useless, because the page is about to be redirected anyway
window.location.href = 'Functions/LogOut.php';
} else if (clickBtnValue == "No") {
$('#delete').val("Delete Account");
}
});
});