简体   繁体   English

SMTP PHP表单成功,但未显示成功消息

[英]SMTP PHP Form success but success message not showing

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? 来自PHP的if(!$ mail-> Send()){甚至无法正常工作,有没有一种方法可以重构它,所以我不需要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> 

You need to enclose your alert like this [Check out the <script> tags around it] 您需要像这样封闭您的alert [查看其周围的<script>标签]

Second thing is that.. You are not properly interpreting the messages , if(!$mail->Send()) it should say Your message has not been sent. 第二件事是..您没有正确解释消息, if(!$mail->Send())它应该说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>";
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM