简体   繁体   English

使用 PHP 和 AJAX 发送电子邮件不起作用

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

I've a contact form on my website.我的网站上有一个联系表。 I want my form to send emails using PHP mail() function without loading the page.我希望我的表单在不加载页面的情况下使用 PHP mail() function 发送电子邮件。 I've tried AJAX but my form submission isn't working.我试过 AJAX 但我的表单提交不起作用。 I've added my HTML code, AJAX code, and PHP code.我添加了我的 HTML 代码、AJAX 代码和 PHP 代码。 What'd the right code?什么是正确的代码? Can anyone please help me to solve this.谁能帮我解决这个问题。 I've tried from many reference sources from google but found no solution.我从谷歌的许多参考资料中尝试过,但没有找到解决方案。

$(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

<?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;
    }
?>

When you submit the form in JavaScript, it's send with the form method (POST) to the server.当您在 JavaScript 中提交表单时,它会使用表单方法(POST)发送到服务器。 The data includes all of your input values.数据包括您的所有输入值。 In this case, the 'submit' input is not "clicked" and therefore not send with the request.在这种情况下,“提交”输入未被“点击”,因此不会随请求一起发送。 So you don't have to check for the submit, means:所以你不必检查提交,意味着:

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

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

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