The user, upon clicking a button(delete) is given the option to delete his account-he must enter his password also to do that. So I am describing above the 1st ajax request which is made.WHat is returned from the server is a response if there are still bookings(it is a booking web app) made from the user and if he should reconsider-a message appears.
If he insists deleting his account then he must click the delete button again. The problem(if you have not noticed) is that by clicking again the delete button the above procedure is repeated while the aim now-after the warning message appears-is that the click of the delete button means "yes, despite the bookings I want to delete my account".
So, this delete button must have dual role, one before the warning appear and one after the warning. What can I do in a situation like this?I was thinking implementing a confirm box but the above are all taking place in a modal box.So a confirm box over a modal I do not think is the correct solution.
Here is the ajax request that is sent when the user first clicks the delete button:
$('.deactivate').click(function(){
event.preventDefault();
var trimmeddelpass=$.trim($('input#del_password').val());
if(trimmeddelpass!=="")
{ $.ajax({
type: "POST",
url: "delaccountajax.php",
data: {"delpass":trimmeddelpass},
success:function(data){
if(data==2)
{$('#warning').html('You have still bookings pending');
$('#warning').show();
}
else if(data==3)
{ window.location.href = "../Frontend/login.php";//proceed deleting the account
}
error:function(jxhr){
alert(jxhr.responseText);}
});
}
Here is the server code:
$output=delete_account($conn,$delpass,$sessionml);
if($output=='2')
{$success='2';}
elseif($output=='3')
{logout();
$success='3';}
echo $success;
Both of the codes above are simplified versions involved since some other stuff were in them too,-they are not needed though for the issue we are dealing.
I have problem implementing the code you propose...it is the right solution of course but...look what is going on,here is the code:
$('.deactivate').click(function(){
event.preventDefault();
deletestate=0;
if(deletestate==0)
{ $.ajax({
type: "POST",
url: "delaccountajax.php",
data: {"delpass":trimmeddelpass},
success:function(data){
if(data==2)
{$('#warning').html('You have booking pending');
$('#warning').show();
deletestate=1
}....
if(deletestate==1){....}
The code above always set the global deletestate to 0,setting it to 1 later has no effect whatsoever...how can i do this?