This is all about to implement a Delete Account feature in my site.
If user requested to delete his account(It takes 3 days to delete the complete data) and try to login within this 3 days. We need to ask a confirmation if you are logged in account will be enabled
For that I am using the below code
$('#loginform').on('submit',function(e) {
var t = $(this);
$.post('/login.php', t.serialize(), function(data) {
if(data.error.length) {
$('.login_result', t).html(data.result).slideDown();
} else if(data.result == 'waiting_for_deletion') {
jconfirm('Account will be enabled if you login',
function(is_confirmed) {
if(is_confirmed) {
$('input:hidden[name="loginform"]').val('confirm_reactivation');
('#loginform').submit();
}
} ,
'Yes', 'Cancel');
} else {
location.href = '/accounts';
}
}, 'json')
});
If user entered correct details (email & password) will ask a confirmation, if he confirmed I am resubmiting the form with a new input field value
I just want to know how Can I reuse my PHP code without submitting the form twice?
or Can I use one more ajax call inside first ajax (I think not possible).
Also it should not affect normal login
// EDITED
Here is my PHP code
login.php
if(isset($_POST['loginform'])) {
$email = trim(mb_strtolower($_POST['email']));
$password = $_POST['upw'];
if (empty($email)) {
$error['#email'] = 'Wrong mail';
}
validate('password', $password, $error['#password']);
// check for correct password now
if(!$error) {
$data = query_execute("SELECT * FROM users WHERE email='".sqlescape($email)."' LIMIT 1");
if(!$data) {
$error['#email'] = 'Account not exists';
} elseif(!$data['active'] && !(int)$data['validated']) {
$error['error'] = 'Account not validated';
} elseif(!$data['password'] || !check_password($password, $data['password'], $data['userid'])) {
$error['#password'] = 'Wrong password';
// ----------------------------------------------
// OK, ALL FINE! USER COULD LOGIN
// ----------------------------------------------
} else {
$confirmation = $_POST['loginform'] == 'confirm_reactivation' ? true : false;
$Delete_Accounts = query_execute("SELECT * FROM deleteacoount WHERE email='".sqlescape($email)."' LIMIT 1");
// Waiting for Deletion accounts
if ($Delete_Accounts[$data['userid']] == 2 && !$confirmation) {
$result = 'waiting_for_deletion';
} else {
if($confirmation) {
query_execute("DELETE FROM deleteacoount WHERE email='".sqlescape($email)."'");
}
DB_query("UPDATE users SET last_login=NOW(),
last_ip='".$_POST['ip']."'
WHERE userid=".$userid."",'#');
$result = 'OK';
}
}
// ----------------------------------------------
}
if($error) {
$result = '<div class="alert alert-block alert-error mb5"><h4>'.$error.'</div>';
}
jsonReturn($result, $error);
}
Here I am using one extra ajax call and reusing existing code to create another PHP file to solve this problem without submitting a form twice.
Script
$(".password").on('focus', function () {
var email=$('input[name="email"]').val();
confirm_reactivation(email);
});
function confirm_reactivation(email)
{
var api="email="+ email;
$.ajax({
method : 'POST',
url : confirm_reactivation.php,
data : api
}).done(function(data){
var out = $.parseJSON(JSON.stringify(data));
if(out.message == 'success'){
$('input:hidden[name="loginform"]').val('confirm_reactivation');
}else{
$('input:hidden[name="loginform"]').val('');
}
});
}
$('#loginform').on('submit',function(e) {
var check_value=$('input:hidden[name="loginform"]').val();
if(check_value !='' || check_value == null)
{
var t = $(this);
$.post('/login.php', t.serialize(), function(data) {
if(data.error.length) {
$('.login_result', t).html(data.result).slideDown();
} else {
location.href = '/accounts';
}
}, 'json')
}
else{
jconfirm('Account will be enabled if you login',
function(is_confirmed) {
if(is_confirmed) {
$('input:hidden[name="loginform"]').val('confirm_reactivation');
//('#loginform').submit();
var t = $(this);
$.post('/login.php', t.serialize(), function(data) {
if(data.error.length) {
$('.login_result', t).html(data.result).slideDown();
} else {
location.href = '/accounts';
}
}, 'json')
}
} ,
'Yes', 'Cancel');
}
});
and here is your new PHP file, I have reused your code and added few things.
//confirm_reactivation.php
<?php
$email = trim(mb_strtolower($_POST['email']));
$Delete_Accounts = query_execute("SELECT * FROM deleteacoount WHERE email='".sqlescape($email)."' LIMIT 1");
// Waiting for Deletion accounts
if ($Delete_Accounts[$data['userid']] == 2) {
$message = array("message" =>'success',"status" =>200);
echo json_encode($message);exit;
}
else{
$message = array("message" =>'failed',"status" =>200);
echo json_encode($message);exit;
}
?>
That's it. Thank you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With