M
M
Mantigoma2021-06-23 12:42:59
PHP
Mantigoma, 2021-06-23 12:42:59

In the phpmailer plugin, set up a redirect when submitting a form?

Please tell me how to make it so that after a successful submission of the form there is a redirect to the thank you page in phpmailer, now the form works, letters arrive, but after sending it redirects to a page with the following text
{"message":"\u0414\u0430\u043d\u043d\u044b \u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b!"}
Here is the handler file code

<?php
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;

  require 'phpmailer/src/Exception.php';
  require 'phpmailer/src/PHPMailer.php';

  $mail = new PHPMailer(true);
  $mail->CharSet = 'UTF-8';
  $mail->setLanguage('ru', 'phpmailer/language/');
  $mail->IsHTML(true);

  //От кого письмо
  $mail->setFrom('[email protected]', 'Заказ потолков');
  //Кому отправить
  $mail->addAddress('[email protected]');
  //Тема письма
  $mail->Subject = 'Заказ обратного звонка';


  //Тело письма
  $body = '<h1>Заказ обратного звонка</h1>';
  
  if(trim(!empty($_POST['user_name']))){
    $body.='<p><strong>Имя:</strong> '.$_POST['user_name'].'</p>';
  }
  if(trim(!empty($_POST['user_phone']))){
    $body.='<p><strong>Телефон:</strong> '.$_POST['user_phone'].'</p>';
  }

  $mail->Body = $body;

  //Отправляем
  if (!$mail->send()) {
    $message = 'Ошибка';
  } else {
    $message = 'Данные отправлены!';
  }

  $response = ['message' => $message];

  header('location: thank-you.html');
  echo json_encode($response);
?>

I tried to delete something, to substitute something, nothing changes, mail comes, but this unnecessary page with text opens. Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad, 2021-06-23
@Mantigoma

You have a response output after the header with a redirect , so the redirect to the desired page does not work Do this, while deleting the last three lines in the code:header('location: thank-you.html')echo json_encode($response)

if (!$mail->send()) {
    $message = 'Ошибка';
    echo $message;
} else {
    header('location: thank-you.html');
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question