防止多次上传Ajax PHP

时间:2017-08-21 05:18:37

标签: php jquery ajax

目前在AJAX上遇到多次上传的问题。

我试过了:

 e.preventDefault();
 e.stopImmediatePropagation();

在我的功能中,但无论如何它都会重复。基本上你可以垃圾邮件提交按钮,并且会发生多次上传。 有什么建议吗?

$(document).ready(function () {

/* Data Insert Starts Here */
$(document).on('submit', '#SavePost', function (e) {
e.preventDefault();
e.stopImmediatePropagation();
    /* Image upload Ajax */
    $.ajax({
        url: "create.php",
        type: "POST",
        data: new FormData(this),
        contentType: false,
        cache: false,
        processData: false,
        beforeSend: function () {

            $("#err").fadeOut();
        },
        success: function (data) {
            if (data == 'invalid file') {
                // invalid file format.
                $("#err").html("Invalid File !").fadeIn();
            } else {
                 $("#dis").fadeOut();
            $("#dis").fadeIn('slow', function () {
                $("#dis").html('<div class="alert alert-info">' + data + '</div>');
                // view uploaded file.
                $("#preview").html(data).fadeIn();
                $("#SavePost")[0].reset();
                $("body").fadeOut('slow', function () {
                    $("body").fadeOut('slow');
                    window.location.href = "memberpage.php";
                 });
            });
            }
        },
        error: function (e) {
            $("#err").html(e).fadeIn();
        }
    });
    /* Image upload Ajax ENDING */
    return false;
});
/* Data Insert Ends Here */

1 个答案:

答案 0 :(得分:0)

使用.one代替.on

确保它只会发射一次。

如果您仍想多次提交,请参阅this