用户登录后将立即提交用户表单

时间:2015-01-24 09:06:38

标签: javascript php ajax login

我是AJAX的新手。我正在开发一个网站,用户可以发布有关该项目的详细信息。用户可以在不登录的情况下将数据插入表单中,但是当用户单击submit时,AJAX脚本将检查用户会话是否存在,如果不存在登录模式,则会显示。

我在这里想说的是,只要用户登录项目表单,就会立即使用AJAX自动提交。

我是这样做的,当用户点击项目表单提交用户会话的脚本检查时,如果会话不存在,登录模式将通过AJAX出现,我在一定时间间隔后设置一个JavaScript计时器,这将检查是否用户是否已登录。

这是我的 jQuery脚本

$(function() {
    $('form').on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            url: 'ajxprocess/checksignin.php',
            success: function(result) {
                if (result == '0') {
                    $.ajax({
                        cache: false,
                        url: 'ajxload/signin_gl.php',
                        success: function(result) {
                            $('#static').modal('show');
                            $('#static').html(result);
                            var myTimer = setInterval(function() {
                                checksignin()
                            }, 500);
                        }
                    });
                } else {
                    $.ajax({
                        type: 'post',
                        url: 'ajxprocess/finalprocess.php',
                        data: $('form').serialize(),
                        beforeSend: function() {
                            $('#final').html("<img src='assets/img/input-spinner.gif' />");
                        },
                        success: function(html) {
                            if (html == '1') {
                                window.location = 'posts.php';
                            } else {
                                $('#final').html('Somthing went wrong during the process!');
                            }
                        }
                    });
                }
            }
        });
    });
});

这是我的登录检查器

function checksignin() {
    $.ajax({
        url: 'ajxprocess/checksignin.php',
        success: function(result) {
            if (result !== '0') {
                $.ajax({
                    type: 'post',
                    url: 'ajxprocess/finalprocess.php',
                    data: $('form').serialize(),
                    beforeSend: function() {
                        $('#final').html("<img src='assets/img/input-spinner.gif' />");

                    },
                    success: function(html) {
                        if (html == '1') {
                            window.location = 'posts.php';
                        } else {
                            $('#final').html('Error occured during the process!');
                        }
                    }
                });
            }
        }
    });
}

问题是我不想使用计时器。有没有其他过程可以做到?

1 个答案:

答案 0 :(得分:0)

您拥有login ajax的成功和错误功能,如果成功则可以提交表单,并在出错时执行其他操作。直到那时你可以显示一个加载符号。这样你就不需要使用计时器了。

相关问题