联系表格提交两次

时间:2016-04-25 15:47:23

标签: javascript php html ajax

我为电子邮件订阅添加了一个弹出窗体,它在提交时发送了两次,确认消息也显示两次

以下是代码:

$(document).ready(function() {
    $(".modalbox").fancybox();
    $("#contact").submit(function(e) {      

        e.preventDefault();                

        var emailval  = $("#email").val();

        var mailvalid = validateEmail(emailval);

        if(mailvalid == false) {
            $("#email").addClass("error");
        }
        else if(mailvalid == true){
            $("#email").removeClass("error");
        }


        if(mailvalid == true) {
            // if both validate we attempt to send the e-mail
            // first we hide the submit btn so the user doesnt click twice
            $("#send").replaceWith("<em>sending...</em>");

            $.ajax({
                type: 'POST',
                url: 'sendmessage.php',
                data: $("#contact").serialize(),
                success: function(data) {
                    if(data == "true") {
                        $("#contact").fadeOut("fast", function(){
                            $(this).before("<p><strong>Success! You have signed up for a trial. A member of our team wil soon be in contact :)</strong></p>");
                            setTimeout("$.fancybox.close()", 1700);
                        });
                    }
                }
            });
        }
    });
});

1 个答案:

答案 0 :(得分:1)

我认为没有必要写这一行:

$("#contact").submit(function() { return false; }); // Remove this

您可以删除此行:

 $("#send").on("click", function(){     // Remove this

然后写下来:

$("#contact").submit(function(e) {      // Add this

    e.preventDefault();                 // Add this

    var emailval  = $("#email").val();

    var mailvalid = validateEmail(emailval);

   /* Other code */

});
相关问题