Answer the question
In order to leave comments, you need to log in
I'm trying to make a feedback form using ajax + PHPMailer. Data is not sent. What to do?
When trying to send data in the browser console, the message "XHR failed loading: POST site/handler.php" appears
when sending something like
<?php
echo $_POST['name'];
?>
<?php
// Файлы phpmailer
require 'mailer/PHPMailer/src/PHPMailer.php';
require 'mailer/PHPMailer/src/SMTP.php';
require 'mailer/PHPMailer/src/Exception.php';
// Переменные, которые отправляет пользователь
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$mail = new PHPMailer\PHPMailer\PHPMailer();
try {
$msg = «ok»;
$mail->isSMTP();
$mail->CharSet = «UTF-8»;
$mail->SMTPAuth = true;
// Настройки вашей почты
$mail->Host = 'smtp.gmail.com'; // SMTP сервера GMAIL
$mail->Username = 'логин@gmail.com'; // Логин на почте
$mail->Password = 'пароль'; // Пароль на почте
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('логин@gmail.com', 'Site'); // Адрес самой почты и имя отправителя
// Получатель письма
$mail->addAddress('логин@mail.ru');
// -----------------------
// Само письмо
// -----------------------
$mail->isHTML(true);
$mail->Subject = 'Заголовок письма';
$mail->Body = "<b>Имя:</b> $name <br>
<b>Почта:</b> $email<br><br>
<b>Сообщение:</b><br>$phone";
// Проверяем отравленность сообщения
if ($mail->send()) {
echo "$msg";
} else {
echo «Сообщение не было отправлено.Неверно указаны настройки вашей почты»;
}
} catch (Exception $e) {
echo «Сообщение не было отправлено. Причина ошибки: {$mail->ErrorInfo}»;
}
?>
Answer the question
In order to leave comments, you need to log in
problem solved. the error was in the validator (a separate file that did not pass data as it should)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question