S
S
slagoris2020-08-25 13:52:33
PHP
slagoris, 2020-08-25 13:52:33

How to make a form submit without reloading?

Greetings!
There is a form on the site. PHPMailer is attached to it. Everything is working. After submitting the form, it goes to a separate page with a thank you form. There is a need to display a thank you form in a pop-up window without switching to another page and without reloading the current one. I added a js popup, after sending it pops up, but although I removed the tail in the php mailer about the location of the transition page, it goes to an empty one when submitting the form. here is the mailer code. Please advise if there is anything to fix here. Or they also told me to do it on Ajax, but I still don’t know, I didn’t understand how, because. newbie.

<?php 
$name = $_POST['user_name'];
$phone = $_POST['user_phone'];
$msg = $_POST['feedback'];

require_once('phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.mail.ru';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '[email protected]';                 // Наш логин
$mail->Password = 'xxxxx;                           // Наш пароль от ящика
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to
 
$mail->setFrom([email protected]', 'XXX');   // От кого письмо 
$mail->addAddress([email protected]');     // Add a recipient
//$mail->addAddress('[email protected]');               // Name is optional
//$mail->addReplyTo('[email protected]', 'Information');
//$mail->addCC('[email protected]');
//$mail->addBCC('[email protected]');
//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'новая заявка с сайта';
$mail->Body    = '
  Пользователь оставил свои данные <br> 
  Имя: ' . $name . ' <br>
  Телефон: ' . $phone . '<br> 
  Сообщение: '. $msg .'';
$mail->AltBody = 'Это альтернативный текст';

if(!$mail->send()) {
    echo 'Error';
} else {
    header('location: ../thanks.html');;
}

?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pavel_mol, 2020-08-25
@slagoris

1) Connect jq
2) Here is an example of submitting a form to the server using ajax

$( "form" ).on( "submit", function( e) {
  e.preventDefault();
  let formParams = $(this).serialize();
  $.ajax({
  url: '/путь_к_твоему_обработчику.php',
  method: 'post',
  dataType: 'json',
  data: formParams ,
  success: function(data){
      //data содержит ответ от сервера, тут показываем попап (если все скрипт отработал успешно)
  }
   });
});

E
Eugene, 2020-08-25
@iamd503

google then phpmailer ajax jquery

T
ThunderCat, 2020-08-25
@ThunderCat

although I removed the tail in the php mailer about the location of the transition page, when submitting the form, it goes to an empty one.
Chiivoo?
Ajax sending code to the studio, it looks like it's not sent by Ajax...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question