所以,我需要一個聯系表單,它可以發送帶有“成功”提示的電子郵件,該提示出現在我的表單中,而無需重新加載。我發現 PHPmailer 有助于將郵件發送到收件箱而不是垃圾郵件(僅此一項工作正常,電子郵件直接發送到收件箱)。然后我添加 JS 驗證,然后是 AJAX,然后是處理 JSON 響應的 PHP。當我單擊“發送”(好?。r出現“成功”提示,但問題是沒有發送/接收電子郵件。沒有 AJAX 的代碼在我的本地和網絡主機上都可以正常工作。如何優化它,以便出現成功提示并發送電子郵件(收件箱,理想情況下)?請不要使用 jQuery 或任何額外的插件。提前致謝。HTML:<div class="contact-form"> <form action="mail.php" method="POST" onsubmit="return doRegister()"> <input id="mail-name" type="text" name="name" placeholder="Your Name"> <span id="name-alert"></span> <input id="mail-email" type="text" name="email" placeholder="Your Email"> <span id="email-alert"></span> <input id="mail-subject" type="text" name="subject" placeholder="Your Subject"> <span id="subject-alert"></span> <textarea id="mail-message" name="message" placeholder="Your Message"></textarea> <span id="message-alert"></span> <input type="submit" value="Send"> <input type="reset" value="Clear"> <span class="contact__form--alert-success" id="success-alert"></span> </form></div>JS:function doRegister() { let checks = { name : document.getElementById("mail-name"), email : document.getElementById("mail-email"), subject : document.getElementById("mail-subject"), message : document.getElementById("mail-message") },
1 回答
FFIVE
TA貢獻1797條經驗 獲得超6個贊
您的 PHP 腳本不會發送郵件,因為send()未調用該函數,因此在驗證后,您應該添加如下內容:
if(empty($error)){
if(!$mail->send()){
$error[] = 'There was an error sending the mail. Please try again!';
}
}
echo json_encode([
"status" => count($error)==0 ? 1 : 0,
"error" => $error
]);
這樣您就可以發送郵件,如果出現問題(send()返回false),則會將錯誤添加到您的錯誤數組中。
- 1 回答
- 0 關注
- 149 瀏覽
添加回答
舉報
0/150
提交
取消
