2016-04-25 16 views
0

Absenden ich ein Pop-up-Formular für E-Mail-Abonnement hinzugefügt und es wird zweimal auf Vorlage senden und die Bestätigungsmeldung auch zweimalKontaktformular zweimal

Hier angezeigt wird, ist der Code:

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

Antwort

1

I don ‚t denke, es gibt keine Notwendigkeit, diese Linie zu schreiben:

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

Sie diese Zeile entfernen:

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

Und schreiben Sie stattdessen:

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

    e.preventDefault();     // Add this 

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

    var mailvalid = validateEmail(emailval); 

    /* Other code */ 

}); 
+0

Thank you very much! Es hat zuerst nicht funktioniert und ich sah mich früher online um und sah ein paar Leute setzen e.stopImmediatePropagation(); unter e.preventDefault(); und das hat für mich funktioniert. Nochmals vielen Dank :) – Ashton

+0

Bitte achten Sie darauf, zu stimmen und die Antwort zu akzeptieren, wenn es Ihr Problem löst. :) –