Friday, 3 August 2018

Reuse the code without submitting the form twice

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);
}



from Reuse the code without submitting the form twice

No comments:

Post a Comment