Ajax没有';t将表单数据发送到我的PHP文件以发送邮件
我正在尝试使用Ajax和phpMail创建一个联系人表单 我已经设法用我的html表单和php文件发送了一封邮件;但是,我希望使用Ajax防止页面重新加载,并显示一个弹出窗口,告诉用户邮件是否已发送。 当我尝试提交表单时,什么也没有发生,当我删除JQuery函数中的事件参数时,页面将永远加载 我尝试在js文件中不包含Ajax的情况下显示弹出窗口,效果很好,因此我假设JQuery库导入正确,问题是Ajax没有将from数据发送到php文件 我的HTML表单:Ajax没有';t将表单数据发送到我的PHP文件以发送邮件,php,jquery,ajax,phpmailer,Php,Jquery,Ajax,Phpmailer,我正在尝试使用Ajax和phpMail创建一个联系人表单 我已经设法用我的html表单和php文件发送了一封邮件;但是,我希望使用Ajax防止页面重新加载,并显示一个弹出窗口,告诉用户邮件是否已发送。 当我尝试提交表单时,什么也没有发生,当我删除JQuery函数中的事件参数时,页面将永远加载 我尝试在js文件中不包含Ajax的情况下显示弹出窗口,效果很好,因此我假设JQuery库导入正确,问题是Ajax没有将from数据发送到php文件 我的HTML表单: <form id="contac
<form id="contact" method="post" action="traitement-formulaire.php">
<div class="form-group">
<div class="form-row">
<div class="col">
<label for="formGroupExampleInput">Nom</label>
<input type="text" name="nom" class="form-control" placeholder="Votre nom" id="nom">
</div>
<div class="col">
<label for="formGroupExampleInput2">Prénom</label>
<input type="text" name="prenom" class="form-control" placeholder="Votre prénom" id="prenom">
</div>
</div>
</div>
<div class="form-group">
<label for="formGroupExampleInput2">Adresse mail</label>
<input type="text" name="mail" class="form-control" id=" mail formGroupExampleInput2" placeholder="ex.: exemple@gmail.com">
</div>
<div class="form-group">
<label for="formGroupExampleInput2">Sujet</label>
<input type="text" name="sujet" class="form-control" id=" sujet formGroupExampleInput2" placeholder="Objet de votre demande">
</div>
<div class="form-group">
<label for="formGroupExampleInput2">Votre message</label>
<textarea type="text" name="message" class="form-control" id=" message formGroupExampleInput2" placeholder="Détaillez votre demande ici..."></textarea>
</div>
<div class="form-group form-actions">
<button class="submit-form btn btn-primary btn-block btn-lg" name="submit" type="submit" value="submit">Envoyer</button>
</div>
</form>
}))
我的PHP文件:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$nom = trim($_POST['nom']);
$prenom = trim($_POST['prenom']);
$mail = trim($_POST['mail']);
$sujet = trim($_POST['sujet']);
$message = trim($_POST['message']);
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ns0.ovh.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'postmaster@sp-neo.com'; // SMTP username
$mail->Password = '*************'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom($mail, $nom);
$mail->addAddress('contact@sp-neo.com'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
// Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $sujet;
$mail->Body = $message;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header( 'Location: index.html' );
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
添加一个答案,这样可能有相同问题的人就不应该浏览评论了 您的第一个问题是,您需要在脚本之后使用slimjquery或加载jQuery 那么到PHPMailer文件的路径是错误的 最后,从表单中读取邮件并将其添加到
$mail
变量中
$mail=trim($_POST['mail'])代码>
但是,然后在同一个变量上初始化PHPMailer对象
$mail=新建PHPMailer(true)代码>
改变
$mail=trim($_POST['mail'])代码>
差不多
$sender=trim($_POST['mail'])代码>
而且还要改变这条线
$mail->setFrom($mail,$nom)代码>
到
$mail->setFrom($sender,$nom)代码>
最后,您有数据类型:'JSON',
,但您的脚本没有用JSON响应
您应该将其更改为数据类型:'text',
,并添加echo“mail sent”
在$mail->send()之后代码>
同时删除标题('Location:index.html')代码>因为它不做任何事情。“所以我假设”-不要假设;核实实际发生的情况。您的浏览器开发工具网络面板可以轻松显示是否正在发出请求。你也可以很容易地调试到你的JS代码中——所以你可以这样做,你能分享一下你面临的错误吗?我听了你的建议,查看了Firefox上的控制台,发现了这个错误:TypeError:$。ajax不是functionmain。js:4:11 jQuery 2类似于你要么在脚本后加载jQuery,要么加载了精简版的jQuery谢谢,这就是问题所在!现在我面临另一个问题,Ajax返回了一个错误,所以我查看了网络工具。POST请求确实已发送,但我得到的错误是500内部服务器错误
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$nom = trim($_POST['nom']);
$prenom = trim($_POST['prenom']);
$mail = trim($_POST['mail']);
$sujet = trim($_POST['sujet']);
$message = trim($_POST['message']);
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ns0.ovh.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'postmaster@sp-neo.com'; // SMTP username
$mail->Password = '*************'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom($mail, $nom);
$mail->addAddress('contact@sp-neo.com'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
// Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $sujet;
$mail->Body = $message;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header( 'Location: index.html' );
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
<!--Alert-->
<div class="alert alert-success" role="alert" id="popup-success">
Votre message a bien été envoyé.
</div>
<div class="alert alert-danger" role="alert" id="popup-error">
Erreur: Votre message n'a pas pu être envoyé.
</div>
<!--Alert-->
#popup-success{
display: none;
}
#popup-error{
display: none;
}