0

My code below works and the form data is sent. However the code to reset the form and show/fade in the success message is not

<?php 
    require("class.phpmailer.php");
    $mail = new PHPMailer();

    // Validation without reload
    if ($_POST) { 

        // SMTP & Sendgrid settings
        $mail->IsSMTP();
        $mail->Host = "smtp.site.com";
        $mail->Port = "587";
        $mail->SMTPAuth = "true";   // Enable SMTP authentication
        $mail->Username = "username";  
        $mail->Password = "password";
        $mail->SMTPSecure = ''; // Enable encryption, 'ssl' also accepted

        // Email headers and body
        $mail->SetFrom("email@email.com");
        $mail->AddAddress("email@email.com");

        $mail->Subject  = "Message from site.com";
        $mail->WordWrap = 50;

        // Form fields
        $FirstName = $_POST['FirstName'];
        $LastName = $_POST['LastName'];
        $Company = $_POST['Company'];
        $JobTitle = $_POST['JobTitle'];
        $Email = $_POST['Email'];
        $Phone = $_POST['Phone'];
        $Message = $_POST['Message'];

        // Assign variables to body. ALWAYS after the variables are assigned.
        $mail->Body     = "You have a new message from your contact form on ShipmentHQ.com \n First Name: $FirstName \n Last Name: $LastName \n Company: $Company \n Job Title: $JobTitle \n Email: $Email \n Phone: $Phone \n Message: $Message";

        if(!$mail->Send()) {
          alert('Thanks, your message has been sent.');
          echo 'Mailer error: ' . $mail->ErrorInfo;
        } 
        else {
          alert('Your message has not been sent.');
        }
    }
?>

What am I doing wrong with this? The if(!$mail->Send()) { from the php doesnt even work, is there a way to refactor it so I don't need the javascript?

<script> 
    $(document).ready(function() {
        $("#ContactForm").validate({
            submitHandler: function() {
                //submit the form
                $.post("<?php echo $_SERVER[PHP_SELF]; ?>", //post
                    $("#ContactForm").serialize(), 
                    function(data){
                      //if message is sent
                      if (data == 'Sent') {
                        $("#message").fadeIn(); //show confirmation message
                        $("#ContactForm")[0].reset(); //reset fields
                    }
                    //
                });
                return false; //don't let the page refresh on submit.
            }
        }); //validate the form
    });
    </script> 
DevKev
  • 5,714
  • 6
  • 24
  • 29

1 Answers1

0

You need to enclose your alert like this [Check out the <script> tags around it]

Second thing is that.. You are not properly interpreting the messages , if(!$mail->Send()) it should say Your message has not been sent. but you are doing the other way. [Don't worry i have modified it for you ;)]

 if(!$mail->Send())
        {
          echo "<script>alert('Your message has not been sent.')</script>";
          echo 'Mailer error: ' . $mail->ErrorInfo;
        } 
 else
        {
     echo "<script>alert('Thanks, your message has been sent.')</script>";
        }
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • same issue, mail sends but no alert. Also see the javascript, I think I need to use it per http://www.johnboy.com/scripts/simple-jquery-form-without-page-refresh/contact.php - @ShankarDamodaran – DevKev Nov 08 '13 at 19:19
  • The page used to show the success message and echo on a white page after reload. Since I added validation I can no longer echo. The javascript is suppose to show/faden the #message div but that doesnt happen either. – DevKev Nov 08 '13 at 19:21
  • There is a similar thread i just found. http://stackoverflow.com/questions/16143911/contact-form-with-phpmailer – Shankar Narayana Damodaran Nov 08 '13 at 19:24
  • Why similar? That is a issue which data being sent. My form sends fine but because of wa I setup validation it just doesnt show thank u message – DevKev Nov 08 '13 at 19:33
  • Try checking the mail server logs at smtp.site.com, which (from the looks of your code) is the SMTP server that you are realying these messages through. Are the logs showing receipt of the message from your application? If so, what happens when it tries to forward the message to the recipient's MX? – mti2935 Nov 09 '13 at 17:25