2017-04-25 99 views
1

我已經創建了一個程序發送附件的電子郵件。首先,我創建它沒有Ajax。然後它正常工作。但是,當我使用jquery ajax因爲不起作用。當我點擊應用按鈕時,什麼都沒有發生我的代碼如下。發送電子郵件與附件使用jQuery的AJAX和PHP

<form action="sendemail.php" enctype="multipart/form-data" method="post"> 
       <h1 class="cta-title">Its a Call To Action</h1> 
       <div class="cta-desc"> 
        <input type="text" value='<?= $row['catogary'];?>' readonly style="width: 75%"><br><br> 
        <input type="text" value='<?= $row['company_name'];?>' readonly style="width: 75%"><br><br> 
        <input type="text" value='<?= $row['location'];?>' readonly style="width: 75%"><br><br> 
        <input type="text" value='<?= $row['qulification'];?>' readonly style="width: 75%"><br><br> 
        <input type="text" value='<?= $row['catogary'];?>' readonly style="width: 75%"><br><br> 
        <input type="text" value='<?= $row['indate'];?>' readonly style="width: 37.5%">&nbsp; 
        <input type="text" value='<?= $row['expdate'];?>' readonly style="width: 37.5%"><br> 
        <input type="text" id="email" name="email" value='<?= $row['email'];?>'><br> 
        <input type="file" name="uploaded_file" id="uploaded_file" class="text-center center-block well well-sm"> 
        <input type="button" id="btn" name="btn" class="btn btn-primary" value="Apply"> 
       </div> 
        <script> 
         $('#btn').click(function() { 
          $.ajax({ 
           method:"POST", 
           url:"sendemail.php", 
           data:{email:$('#email').val()}, 
           success:function (data) { 
            alert(data); 
            return false; 
           } 
          }); 
         }); 
        </script> 
       </form> 

sendmail.php

<?php 
    $email2=$_POST['email']; 
    require_once('PHPMailer/PHPMailerAutoload.php'); 
    $mail = new PHPMailer; 
    $mail->IsSMTP(); // enable SMTP 
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only 
$mail->SMTPAuth = true; // authentication enabled 
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail 
$mail->Host = "smtp.gmail.com"; 
$mail->Port = 465; // or 587 
$mail->IsHTML(true); 
$mail->Username = "[email protected]"; 
$mail->Password = "uwucst14xxxx"; 
$mail->SetFrom("[email protected]"); 
$mail->FromName = "Internship Management"; 

$mail->addAddress($email2); 
/
$mail->addReplyTo("[email protected]", "Reply"); 

$mail->isHTML(true); 

$mail->Subject = 'CV for internship Vacancy'; 
$mail->Body = "Attached"; 
if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) { 

    $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],$_FILES['uploaded_file']['name']); 
} 

if(!$mail->send()) 
{ 
    echo "Mailer Error: " . $mail->ErrorInfo; 

} 
else 
{ 
    echo 'Successfully Applied for vacancy'; 
} 
?> 
+0

[您是否觀看過瀏覽器開發人員工具中的AJAX請求/響應?你有沒有在項目中包含jQuery庫?是否有任何錯誤報告?你在網絡服務器上運行?](http://jayblanchard.net/basics_of_jquery_ajax.html) –

+0

是的,我正在運行this.nothing發生。但在使用ajax之前,這是運行well.yes.jquery庫被使用和沒有錯誤報告。 –

+0

更改應用按鈕類型提交 – Akintunde007

回答

0

這裏是我的實現。我改變了一些屬性。但需要注意的重要,我的SSL更改我的目的,TLS和爲587

<!DOCTYPE html> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 

</head> 

<body> 


    <form onsubmit="return submitForm();" enctype="multipart/form-data" method="post" name="fileinfo" id="fileinfo" > 
       <h1 class="cta-title">Its a Call To Action</h1> 
       <div class="cta-desc"> 
        <input type="text" value='a' readonly style="width: 75%"><br><br> 
        <input type="text" value='b' readonly style="width: 75%"><br><br> 
        <input type="text" value='c' readonly style="width: 75%"><br><br> 
        <input type="text" value='d' readonly style="width: 75%"><br><br> 
        <input type="text" value='e' readonly style="width: 75%"><br><br> 
        <input type="text" value='f' readonly style="width: 37.5%">&nbsp; 
        <input type="text" value='g' readonly style="width: 37.5%"><br> 
        <input type="text" id="email" name="email" value='[email protected]'><br> 
        <input type="file" name="files" id="files" class="text-center center-block well well-sm"> 
        <input type="submit" id="btn" name="btn" class="btn btn-primary" value="Apply"> 
       </div> 
        <script> 
         function submitForm() { 

         console.log("submit event"); 

         var fd = new FormData(document.getElementById("fileinfo")); 
         fd.append("label", "WEBUPLOAD"); 

         $.ajax({ 
          url: "mailattach.php", 
          type: "POST", 
          data: fd, 
          processData: false, // tell jQuery not to process the data 
          contentType: false // tell jQuery not to set contentType 
         }).done(function(data) { 
           console.log("PHP Output:"); 
           console.log(data); 
          }); 
          return false; 
         } 
        </script> 
       </form> 


</body> 
</html> 

因此,在PHP太多,我改變了upload_file到文件中。 mailattach中的php代碼與您提供的相同

+0

這也不工作的朋友 –

+0

提供錯誤消息。另外,如果您在非https連接下運行。將ssl更改爲tls,將端口更改爲587。使用gmail進行測試,但您必須登錄gmail帳戶,並在「不安全」連接下更改發送電子郵件的權限 –