我正在用phpmailer发送电子邮件,当我按下;发送(&Q);按钮,但我想保持在相同的HTML页面上,所以我将AJAX脚本与HTML和PHP结合使用。问题是,我没有得到用电子邮件状态和PHP响应更新HTML页面所需的JSON数据,因此:
感谢您的帮助
HTML中的AJAX
<script>
$("#form").submit(function(e) {
e.preventDefault(); // prevent actual form submit
var from_name_temp = $(\'#from_name\').val();
var from_email_temp = $(\'#from_email\').val();
var choix_gout_temp = $(".choix_gout:checked").val();
$.ajax({
type: "POST",
url: "/mailer.php",
data: "from_name="+from_name_temp+"&from_email="+from_email_temp+"&choix_gout="+choix_gout_temp,
dataType: "json",
complete: function(data) {
console.log("Thank you for subscribing!");
console.log(data);
console.log(data.status);
console.log(data.message);
console.log(data.success);
}
})
});
</script>
PHP
<?php
$error = \'\';
$name = \'\';
$email = \'\';
$subject = \'\';
$message = \'\';
$from_email = \'\';
$from_name = \'\';
$choix_gout = \'\';
$response_array = array();
use PHPMailer\\PHPMailer\\PHPMailer;
use PHPMailer\\PHPMailer\\Exception;
function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}
send($from_name, $from_email, $choix_gout);
function send($from_name, $from_email, $choix_gout){
$error = \'\';
$ot1 = $_POST[\'choix_gout\'];
$ot2 = $_POST[\'from_name\'];
$ot3 = $_POST[\'from_email\'];
echo($ot1);
echo($ot2);
echo($ot3);
if(empty($ot2))
{
$error .= \'<p><label class="text-danger">Please Enter your Name</label></p>\';
$name = \'\';
}
else
{
$name = clean_text($ot2);
if(!preg_match("/^[a-zA-Z ]*$/",$name))
{
$error .= \'<p><label class="text-danger">Only letters and white space allowed</label></p>\';
}
}
if(empty($ot3))
{
$error .= \'<p><label class="text-danger">Please Enter your Email</label></p>\';
$email = \'\';
}
else
{
//$email = clean_text($_POST["from_email"]);
$email = clean_text($ot3);
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$error .= \'<p><label class="text-danger">Invalid email format</label></p>\';
}
}
if($error == \'\')
{
require \'PHPMailer/src/Exception.php\'; /* Exception class. */
require \'PHPMailer/src/PHPMailer.php\'; /* The main PHPMailer class. */
require \'PHPMailer/src/SMTP.php\'; /* SMTP class, needed if you want to use SMTP. */
require \'PHPMailer/src/class.html2text.php\';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = "1";
$mail->Host = "*****";
$mail->Port = "587";
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = "true";
$mail->Username = "*****";
$mail->Password = "*****";
$mail->AddReplyTo("*****","****");
$mail->From = ("****");
$mail->FromName = ("***");
$mail->AddAddress("*****,******");
$mail->Subject = "[GOUT] : " . $ot1 . \';\' . $name . \';\' . $email;
$mail->IsHTML(true);
$mail->Body = "
<div style=\'width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;\'>
</div>
";
if(!$mail->Send()) {
$message = \'Mail error: \'.$mail->ErrorInfo;
$response_array = array("success"=> false,
"status" => false,
"message" => $message);
header(\'Content-Type: application/json\');
echo json_encode($response_array);die();
} else {
$message = "email sent";
$response_array = array("success"=> true,
"status" => true,
"message" => $message);
header(\'Content-Type: application/json\');
echo json_encode($response_array);die();
}
}
else{
$response_array = array("success"=> false,
"status" => false,
"message" => $error);
header(\'Content-Type: application/json\');
echo json_encode($response_array);die();
}?>