【问题标题】:Sending e-mail using PHP and AJAX not working使用 PHP 和 AJAX 发送电子邮件不起作用
【发布时间】:2020-08-30 13:40:56
【问题描述】:

我的网站上有一个联系表。我希望我的表单使用 PHP mail() 函数发送电子邮件而不加载页面。我尝试过 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;
    }
?>

【问题讨论】:

  • 你有没有报错控制台(Ctrl+Maiusc+i)/php?
  • 将错误日志添加到您的 PHP 并在控制台中查找 JS
  • 是的,我添加了错误日志,但控制台中没有显示错误

标签: php jquery ajax forms email


【解决方案1】:

当您在 JavaScript 中提交表单时,它会通过表单方法 (POST) 发送到服务器。数据包括您的所有输入值。在这种情况下,“提交”输入未被“点击”,因此不会随请求一起发送。所以你不必检查提交,意味着:

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

【讨论】:

    猜你喜欢
    • 2018-10-30
    • 2011-06-06
    • 1970-01-01
    • 2021-12-31
    • 2015-08-10
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多