V
V
Victoria2021-11-25 15:12:00
AJAX
Victoria, 2021-11-25 15:12:00

How to show pop-up after form submission?

What to add in php to display pop-up message?

async function form_submit(e) {
  let btn = e.target;
  let form = btn.closest('form');
  let error = form_validate(form);
  if (error == 0) {
    let formAction = form.getAttribute('action') ? form.getAttribute('action').trim() : '#';
    let formMethod = form.getAttribute('method') ? form.getAttribute('method').trim() : 'GET';
    const message = form.getAttribute('data-message');
    const ajax = form.getAttribute('data-ajax');

    //SendForm
    if (ajax) {
      e.preventDefault();
      let formData = new FormData(form);
      form.classList.add('_sending');
      let response = await fetch(formAction, {
        method: formMethod,
        body: formData
      });
      if (response.ok) {
        let result = await response.json();
        form.classList.remove('_sending');
        if (message) {
          popup_open(message + '-message');
        }
        form_clean(form);
      } else {
        alert("Ошибка");
        form.classList.remove('_sending');
      }
    }
    // If test
    if (form.hasAttribute('data-test')) {
      e.preventDefault();
      if (message) {
        popup_open(message + '-message');
      }
      form_clean(form);
    }
  } else {
    let form_error = form.querySelectorAll('._error');
    if (form_error && form.classList.contains('_goto-error')) {
      _goto(form_error[0], 1000, 50);
    }
    e.preventDefault();
  }
}


<?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['name']))){
    $body.='<p><strong>Имя:</strong> '.$_POST['name'].'</p>';
  }
  if(trim(!empty($_POST['email']))){
    $body.='<p><strong>E-mail:</strong> '.$_POST['email'].'</p>';
  }
  if(trim(!empty($_POST['message']))){
    $body.='<p><strong>Сообщение:</strong> '.$_POST['message'].'</p>';
  }

  $mail->Body = $body;

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

  header('Content-type: application/json');
  echo json_encode($response);
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pLavrenov, 2021-11-25
@pLavrenov

fancybox api

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question