The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> Need Confirmation Message to Remain on Page
post Aug 22 2019, 09:03 AM
Post #1


Group: Members
Posts: 19
Joined: 6-April 16
Member No.: 24,129

I have a working php contact form:

$msg = "";
use PHPMailer\PHPMailer\PHPMailer;
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";
include_once "PHPMailer/SMTP.php";

if (isset($_POST['submit'])) {
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];

$mail = new PHPMailer();

//if we want to send via SMTP
$mail->Host = "";
$mail->SMTPAuth = true;
$mail->Username = "email";
$mail->Password = "password";
$mail->SMTPSecure = "ssl"; //TLS
$mail->Port = 465; //587

$mail->Subject = $subject;
$mail->Body = $message;

if ($mail->send())
echo "<h1>Sent Successfully! Thank you"." ".$name.", We will contact you shortly!</h1></br>
Use the 'back' arrow to go back to the main page...";
echo "Something went wrong!";

The confirmation message is echoed out to a new, white page. Still getting to grips with php, but I cannot work out how to get the confirmation message to appear above the form instead of appearing on a new page.

This is the form's code

<form method="post" action="contact.php" enctype="multipart/form-data">
<input name="subject" placeholder="Subject..."><br>
<input name="email" type="email" placeholder="Email..."><br>
<textarea placeholder="Message..." name="message"></textarea><br>
<input name="submit" type="submit" value="Send Email">

Any ideas?

Hope someone can help.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Reply to this topicStart new topic
post Aug 23 2019, 07:48 PM
Post #2

Programming Fanatic

Group: Members
Posts: 1,677
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088

Makes sense, I've never used PHPMailer before so I skipped over those parts. Anyway, I don't need to run the code.

In the zip I found 'test.php', 'contact.php' and the PHPMailer directory. test.php contains the HTML form (which is invalid but I fixed it) and contact.php is the code that processes the test.php form data. In contact.php you use a variable named '$name' but I can't find it defined anywhere. Also, I don't see any code to validate the user data (other than the required attribute). HTML/Javascript can be altered by the user so you should always do data validation on the server (PHP in this case). Remember to always plan for the bad guys. In this case there is no database connection but you should at least check to make sure there is no missing data.

Attached are your 2 files. I changed nothing as far as PHPMailer goes. Make your changes to PHPMailer then load test.php and try it.
Attached File  test.php ( 643bytes ) Number of downloads: 167
Attached File  contact.php ( 914bytes ) Number of downloads: 160
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic

Reply to this topicStart new topic
3 User(s) are reading this topic (3 Guests and 0 Anonymous Users)
0 Members:


- Lo-Fi Version Time is now: 18th November 2019 - 04:36 PM