0

I'm working with a contact form. The PHP code successfully delivers the form data but when I submit the form I get a log display on a new page (the form.php page), and the response message is printed at the bottom of the log and doesn't alert. Also, I would like the form page to simply reload and not redirect to the .php page. This is my first time using php so any help would be greatly appreciated. See attached image, and php below:


<form id="form-id" class="form-class" name="Form" method="post" action="form.php" onsubmit="return">

<?php

$name = $_POST['name'];
$role = $_POST['role'];
$company = $_POST['company'];
$country = $_POST['country'];
$email = $_POST['email'];
$message = $_POST['message'];


   $mail = new PHPMailer;
   $mail->isSMTP();
   $mail->SMTPDebug = 2;
   $mail->Host = '';
   $mail->Port = 'num';
   $mail->SMTPAuth = true;
   $mail->Username = 'username';
   $mail->Password = 'password';
   $mail->setFrom('email');
   $mail->addReplyTo('email');
   $mail->addAddress('email', 'Name');
   $mail->Subject = 'New Message: '.$organisation;
   $mail->Body = 
   'Name: '.$name."\r\n".
   'Role: '.$role."\r\n".
   'Company: '.$company."\r\n".
   'Country: '.$Country."\r\n".
   'Email: '.$email."\r\n".
   'Message: '.$message.


if ($mail->send()) {
  echo ("Thank you " .$name. ". Message sent.");
} 
?>

<div id="center_button"><button onclick="location.href='team&contact#contactus'">Return</button></div>

Chi
  • 1
  • 3
  • Just `die()` after `echo`. – Marcin Orlowski Jan 06 '23 at 20:41
  • Make the PHP page [redirect back to the form page](https://stackoverflow.com/a/768472/4705262). For the user, this will appear more like a "refresh", it also prevents the user from accidentally resubmitting the form multiple times in a row. – GrumpyCrouton Jan 06 '23 at 21:41
  • Thanks guys. The page is now reloading and the log is no longer displayed. But I'm still not getting the alert / echo to tell the user that the form has been sent. Is there something wrong with my syntax? – Chi Jan 07 '23 at 06:51

0 Answers0