【问题标题】:Send email without page refresh PHP AJAX. The email.php sends email action is in HTML发送电子邮件而不刷新页面 PHP AJAX。 email.php 发送电子邮件操作是 HTML
【发布时间】:2020-05-15 00:55:40
【问题描述】:

我想用 AJAX 发送它。我哪里错了?我得到的错误是:

主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响。如需更多帮助,请查看https://xhr.spec.whatwg.org/.send@jquery.min.js:4

$(document).ready(function() {
  $("#submit-email").click(function(event) {
    var email = document.getElementById('mail').value;
    var message = document.getElementById('message').value;
    var dataString = {
      "mail": email,
      "message": message
    }

    $.ajax({
      type: "post",
      url: "controller/email.php",
      data: dataString,
      success: function(html) {
        alert('Success!');
      }
    });
    event.preventDefault();
  });
});
<form>
  <input type="email" name="mail" id="mail" required="required" class="form" placeholder="Email" />
  <input type="hidden" name="message" id="message" value="test value" />
  <button type="submit" id="submit-email" name="submit" class="submit-email">Send Message</button>
</form>
<?php

if(isset($_POST['submit']) ){ 

$youremail = 'hello@blahblah.net';   
$user_email = $_POST['mail'];
$message = $_POST['message'];

$and = 'blahblah.net';
$body = "You requested we email the following to you: $message. Regards, Team";

$headers = "From: $youremail"; 

mail($user_email, 'blahblah.net', $body, $headers ); 

}

$newURL = '../index-open.php';
header('Location: '.$newURL);


?>

【问题讨论】:

  • 你是说页面刷新了?因为它不会在您的问题中给出 JS(由于event.preventDefault())。此外,该错误与显示的代码无关,因为它是关于同步请求的警告,但您使用的代码是异步的 - 除非您在全局 AJAX 设置中设置了async: false,在这种情况下只需删除它财产。
  • 使用 FormData() 设置对象数据
  • @Rory McCrossan 我没有触及 ajax 设置,我相信默认情况下是正确的。知道如何检查吗?根据 Zeeshan 的建议编辑后,它没有刷新。

标签: jquery html ajax


【解决方案1】:

试试这个:

这可能会有所帮助。

$(document).ready(function() {
  $("form").submit(function(e) {
    e.preventDefault();
    var email = document.getElementById('mail').value;
    var message = document.getElementById('message').value;
    var dataString = {
      "mail": email,
      "message": message
    }

    $.ajax({
      type: "post",
      url: "controller/email.php",
      data: dataString,
      success: function(html) {
        alert('Success!');
      }
    });

  });
});

【讨论】:

  • 谢谢,虽然它不发送电子邮件,但它确实显示了警报。
  • 在这种情况下,您的 JS 正在正确发送请求,而问题现在在您的 email.php 文件中。你需要调试它
  • 哦,所以我只是在 php 中使用 mail() &lt;?php if(isset($_POST['submit']) ){ $youremail = 'hello@blahblah.net'; $user_email = $_POST[mail]; $stand = 'blahblah.net'; $body = "You requested we email the following to you: $_POST[message]. Regards, Team"; $headers = "From: $youremail"; mail($user_email, 'blahblah.net', $body, $headers ); } $newURL = '../index-open.php'; header('Location: '.$newURL); ?&gt;
  • @technicalDifficulties $_POST 变量是一个关联数组,您需要使用字符串访问其属性,即添加引号:$_POST['mail']$_POST['message']
  • @Quangdao Nguyen 谢谢,我已经更改并编辑了帖子。甚至没有注意到,因为它发送的 HTML 表单。出于某种原因,它仍然没有通过 Ajax 发送。
猜你喜欢
  • 1970-01-01
  • 2021-01-26
  • 2011-09-16
  • 1970-01-01
  • 2015-01-01
  • 2014-04-29
  • 2012-11-11
  • 2021-11-21
  • 2011-06-25
相关资源
最近更新 更多