簡體   English   中英

使用 PHP 和 AJAX 發送電子郵件不起作用

[英]Sending e-mail using PHP and AJAX not working

我的網站上有一個聯系表。 我希望我的表單在不加載頁面的情況下使用 PHP mail() function 發送電子郵件。 我試過 AJAX 但我的表單提交不起作用。 我添加了我的 HTML 代碼、AJAX 代碼和 PHP 代碼。 什么是正確的代碼? 誰能幫我解決這個問題。 我從谷歌的許多參考資料中嘗試過,但沒有找到解決方案。

$(function() {
  $("#ContactNorthstarHHC").submit(function(e) {
    e.preventDefault();
    var form_data = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "ContactNorthstarHHC.php",
        dataType: "json",
        data: form_data,
      })
      .done(function(data) {
        $("#response").html("Thanks for your message.");
        $("#response").addClass("alert alert-success");
      })
      .fail(function(data) {
        $("#response").html("Sorry! Message not sent.");
        $("#response").addClass("alert alert-danger");
      });
  });
});
<form name="ContactNorthstarHHC" id="ContactNorthstarHHC" method="POST">
  <div class="form-group mb-4">
    <input type="text" name="fullName" class="contact-input form-control" placeholder="Full Name" required>
  </div>
  <div class="form-group mb-4">
    <input type="email" name="emailAddr" class="contact-input form-control" placeholder="Email Address" required>
  </div>
  <div class="form-group mb-4">
    <input type="text" name="phoneNo" class="contact-input form-control" placeholder="Phone no" required>
  </div>
  <div class="form-group mb-4">
    <select class="contact-input form-control custom-select" name="Gender" required>
      <option value="">Gender</option>
      <option value="Male">Male</option>
      <option value="Female">Female</option>
      <option value="Other">Other</option>
    </select>
  </div>
  <div class="form-group mb-4">
    <input type="text" name="Message" class="contact-input form-control" placeholder="Enter Your Massage" required>
  </div>
  <div class="form-group mb-4 text-center">
    <input type="submit" class="text-white site-btn btn py-2 px-5" value="Contact Now" name="ContactNow">
  </div>
  <div id="response"></div>
</form>

PHP

<?php
    if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["ContactNow"])){
        $cont_name = cont_data($_POST['fullName']);
        $cont_mail = cont_data($_POST["emailAddr"]);
        $cont_phon = cont_data($_POST['phoneNo']);
        $cont_gndr = cont_data($_POST["Gender"]);
        $cont_msg = cont_data($_POST["Message"]);

        if(isset($cont_name) && isset($cont_phon) && isset($cont_mail) && isset($cont_gndr) && isset($cont_msg)){
            $to = 'mazumdernazmul00@gmail.com';
            $subject = 'Contact request from' . $cont_mail;
            $message = "$cont_name\n $cont_mail\n $cont_phon\n $cont_gndr\n $cont_msg";
            $headers  = "Content-type: text/html; charset=utf-8 \r\n"; 
            $headers .= "From: $cont_mail\r\n"; 

            mail($to, $subject, $message, $headers);
            echo json_encode(array('status' => 'success'));

        }else{
            echo json_encode(array('status' => 'error'));
        }
    }

    function cont_data($data){
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
?>

當您在 JavaScript 中提交表單時,它會使用表單方法(POST)發送到服務器。 數據包括您的所有輸入值。 在這種情況下,“提交”輸入未被“點擊”,因此不會隨請求一起發送。 所以你不必檢查提交,意味着:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){ // <-- remove  '&& isset($_POST["ContactNow"])'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM