首页 > 解决方案 > 重复使用代码而不提交表单两次

问题描述

这一切都将在我的站点中实现删除帐户功能。

如果用户要求删除他的帐户(删除完整数据需要 3 天)并尝试在这 3 天内登录。我们需要确认您是否已登录帐户将被启用

为此,我正在使用以下代码

 $('#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')
});

如果用户输入了正确的详细信息(电子邮件和密码)将要求确认,如果他确认我将使用新的输入字段值重新提交表单

我只想知道如何在不提交表单两次的情况下重用我的 PHP 代码?

或者我可以在第一个 ajax 中再使用一个 ajax 调用吗(我认为不可能)。

也不应该影响正常登录

// 已编辑

这是我的PHP代码

登录.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);
}

标签: javascriptphpjqueryajaxforms

解决方案


您需要为ajax编写函数,并在修改对象后 - 再次提交

function submitForm(form) {
    $.post('/ajax.php', form.serialize(), function (data) {
        if (data.error.length) {
            $('.login_result', form).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) {
                        form.find('input:hidden[name="loginform"]').val('confirm_reactivation');
                        submitForm(form);
                        return;
                    }
                },
                'Yes', 'Cancel');
        } else {
            location.href = '/me';
        }
    });
}

$('#loginform').on('submit', function (e) {
    e.preventDefault();

    submitForm($(this));
});

推荐阅读