Search code examples
phpcontact-formzurb-foundation-5

Multiple emails in Foundation 5 feedback form


I'm very much a beginner, but I've managed to put together a feedback form for my website. The problem I'm having is that the form is sending 2 emails rather than 1. Any thoughts on what is causing this?

$('#myForm') 
    .on('valid.fndtn.abide', function () {
    var name = $("input#name").val();
    var email = $("input#email").val();
    var message = $("textarea#message").val();

    var dataString = 'name=' + name +
          '&email=' + email +
          '&message=' + message;

    $.ajax({
      type: "POST",
      url: "/contact/mail.php",
      data: dataString,
      success: function() {
        $('.contactform').html("<div id='thanks'></div>");
          $('#thanks').html("<h2>Thanks!</h2>")
          .append("<p>Dear "+ name +"!, We will get back to you as soon as we can.</p>")
          .hide()
          .fadeIn(1500);
      },
    });  //ajax call
    return false;
 });

PHP:

<?php

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

$msg = "

Name: $name
Email: $email
Comments:
$message
";

$to = "[email protected]";
$subject = "Website";
$message = $msg;
$headers = "Website";
mail($to,$subject,$message,$headers);

?>

Solution

  • Apparently Foundation 5 still contains an old call function from a prior release "valid" vs "valid.fndtn.abide" which is causing it to be called twice. I was able to prevent the double emails by making the following changes:

    $('#myForm') 
    .on('valid.fndtn.abide', function (e) {
      if(e.namespace != 'abide.fndtn') {
        return;
      }
      var name = $("input#name").val();
      var email = $("input#email").val();
      var message = $("textarea#message").val();