2016-11-21 105 views
0
<!DOCTYPE html> 
<html lang="en"> 
<head> 

    <title>AJAX Contact Form Demo</title> 

    <link rel="stylesheet" href="style.css"> 
</head> 
<body> 
    <div id="page-wrapper"> 
     <h1>AJAX Contact Form Demo</h1> 

     <div id="form-messages"></div> 

     <form id="main-contact-form" method="post" action="mailer.php"> 
      <div class="field"> 
       <label for="name">Name:</label> 
       <input type="text" class="form-control" id="name" name="name" required> 
      </div> 

      <div class="field"> 
       <label for="email">Email:</label> 
       <input type="email" class="form-control" id="email" name="email" required> 
      </div> 

      <div class="field"> 
       <label for="message">Message:</label> 
       <textarea id="message" class="form-control" name="message" required></textarea> 
      </div> 

      <div class="field"> 
       <button type="submit" name="submit">Send</button> 
      </div> 
     </form> 
    </div> 

    <script src="jquery-2.1.0.min.js"></script> 
    <script type="text/javascript" src="pap.js"></script> 
</body> 
</html> 

這是我的Ajax代碼:PHP AJAX梅勒問題

$(function() { 

    // Get the form. 
    var form = $('#main-contact-form'); 

    // Get the messages div. 
    var formMessages = $('#form-messages'); 

    // Set up an event listener for the contact form. 
    $(form).submit(function(e) { 
     // Stop the browser from submitting the form. 
     e.preventDefault(); 

     // Serialize the form data. 
     var formData = $(form).serialize(); 

     // Submit the form using AJAX. 
     $.ajax({ 
      type: 'POST', 
      url: $(form).attr('action'), 
      data: formData 
     }) 

     .done(function(response) { 
      // Make sure that the formMessages div has the 'success' class. 
      $(formMessages).removeClass('error'); 
      $(formMessages).addClass('success'); 

      // Set the message text. 
      $(formMessages).text(response); 

      // Clear the form. 
      $('#name').val(''); 
      $('#email').val(''); 
      $('#message').val(''); 

     }) 
     .fail(function(data) { 
      // Make sure that the formMessages div has the 'error' class. 
      $(formMessages).removeClass('success'); 
      $(formMessages).addClass('error'); 

      // Set the message text. 
      if (data.responseText !== '') { 
       $(formMessages).text(data.responseText); 
      } else { 
       $(formMessages).text('Oops! An error occured and your message could not be sent.'); 
      } 
     }); 

    }); 

}); 

這裏是我的PHP梅勒代碼:

<?php 


if(isset($_POST['submit'])) 
{ 
     $name = strip_tags(trim($_POST["name"])); 
     $name = str_replace(array("\r","\n"),array(" "," "),$name); 
     $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL); 
     $message = trim($_POST["message"]); 


$mess= 
'Full Name: $name<br /> 
Email: $email<br /> 
Comments: $message<br /> 
'; 

    require "phpmailer/class.phpmailer.php"; //include phpmailer class 

    // Instantiate Class 
    $mail = new PHPMailer(); 

    // Set up SMTP 
    $mail->IsSMTP();    // Sets up a SMTP connection 
    $mail->SMTPAuth = true;   // Connection with the SMTP does require authorization  
    $mail->SMTPSecure = "ssl";  // Connect using a TLS connection 
    $mail->Host = "smtp.gmail.com"; //Gmail SMTP server address 
    $mail->Port = 465; //Gmail SMTP port 
    //$mail->Encoding = '7bit'; 

    // Authentication 
    $mail->Username = "Enter Your Email-ID For Testing"; // Your full Gmail address 
    $mail->Password = "Your Email Password"; // Your Gmail password 

    // Compose 
    $mail->SetFrom($_POST['email'], $_POST['name']); 
    $mail->AddReplyTo($_POST['email'], $_POST['name']); 
    $mail->Subject = "hello";  // Subject (which isn't required) 
    $mail->MsgHTML($mess); 
    echo"hello"; 
    // Send To 
    $mail->AddAddress("[email protected]", "Bla Bla"); // Where to send it - Recipient 
    $result = $mail->Send();  // Send! 
    $mess = $result ? 'Successfully Sent!' : 'Sending Failed!';  
    unset($mail); 
    echo 'Successfully Sent!'; 

} 

?> 

我認爲這是在$亂七八糟的關鍵字的問題,但我想將這些前綴與發送郵件中的詳細信息連接起來。

但是,如果我糾正,也之後也郵件不會。

有人能幫我解釋爲什麼會出現這個問題。

請使用phpmailer類,因爲它需要發送郵件。

感謝您的幫助。

+0

什麼價值$ _ POST [ '提交']?你有什麼錯誤? – madalinivascu

+0

@madalinivascu:這段代碼沒有錯誤。但郵件不會。 Ajax存在一些問題,因爲它沒有調用mailer.php。 –

+0

你怎麼知道你不叫mailer.php? – madalinivascu

回答

0

如果您使用的是本地服務器,如XAMPP,WAMP,LAMP。也許你必須在你的服務器的php.ini上配置SMTP

如果你在網絡服務器上部署你的應用程序,檢查你的gmail帳戶是否已經激活允許「不太安全的應用程序」通過你的帳戶連接的權限。

您可以使用PHPMailer的調試跟蹤在日誌中的錯誤,併爲我們帶來有關該問題的更多信息:

$ MAIL-> SMTPDebug = 2; //使SMTP調試信息(用於測試) // 1 =錯誤和消息 // 2 =僅信息