The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Need Confirmation Message to Remain on Page
chome4
post Aug 22 2019, 09:03 AM
Post #1


Newbie
*

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



I have a working php contact form:

<?php
$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 = "smtp.34sp.com";
//$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Username = "email";
$mail->Password = "password";
$mail->SMTPSecure = "ssl"; //TLS
$mail->Port = 465; //587

$mail->addAddress('email');
$mail->setFrom($email);
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = $message;
//$mail->addAttachment($file);

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...";
else
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">
</form>



Any ideas?

Hope someone can help.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Aug 22 2019, 12:46 PM
Post #2


Programming Fanatic
********

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



I'll start by giving you hints, if you need help with the code just post back again.

1. You need a container to hold the message (maybe a DIV?). Place it above the form tag.
2. Assign your message to a variable.
3. Use the 'header' command to redirect to the same contact page.
4. Echo out the message into the DIV.

These are the basic steps but of course you need to add a little more.


Post back, with current code, if you need any more help.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Aug 22 2019, 04:49 PM
Post #3


Programming Fanatic
********

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



I had some time so I put together an example page. You will have to adjust the code to fit your logic but it should get you going.
The form has 1 input, leave it blank and click Submit. An error message will now appear above the form.
Next, type some text into the input and click Submit. A success message will now appear above the form and the form will be blank.
Attached File  example.php ( 874bytes ) Number of downloads: 163


This post has been edited by CharlesEF: Aug 22 2019, 04:55 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
chome4
post Aug 23 2019, 01:19 AM
Post #4


Newbie
*

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



QUOTE(CharlesEF @ Aug 22 2019, 04:49 PM) *

I had some time so I put together an example page. You will have to adjust the code to fit your logic but it should get you going.
The form has 1 input, leave it blank and click Submit. An error message will now appear above the form.
Next, type some text into the input and click Submit. A success message will now appear above the form and the form will be blank.
Attached File  example.php ( 874bytes ) Number of downloads: 163



Working on it....

Incorporating it into the existing page is giving some formatting issues but will let you know when it's behaving itself.....
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
chome4
post Aug 23 2019, 08:52 AM
Post #5


Newbie
*

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



Still can't get it working!

Attached is a working copy of the current contact form. After the submit button has been clicked, confirmation is echoed onto a new page. I've tried putting the echo command in div tags above the form but just get a blank page.

I'm well out of my depth on this.


Attached File(s)
Attached File  contact.zip ( 51.15k ) Number of downloads: 144
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Aug 23 2019, 11:18 AM
Post #6


Programming Fanatic
********

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



When I try to load the contact.php page I get this error: [23-Aug-2019 16:12:08 UTC] PHP Parse error: syntax error, unexpected '[' in F:\xxxx\xxxx\xxxx\test\PHPMailer\PHPMailer.php on line 288

I have to leave right now but I'll look at the code tonight.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
chome4
post Aug 23 2019, 12:20 PM
Post #7


Newbie
*

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



QUOTE(CharlesEF @ Aug 23 2019, 11:18 AM) *

When I try to load the contact.php page I get this error: [23-Aug-2019 16:12:08 UTC] PHP Parse error: syntax error, unexpected '[' in F:\xxxx\xxxx\xxxx\test\PHPMailer\PHPMailer.php on line 288

I have to leave right now but I'll look at the code tonight.


In 'contact.php', lines 21, 22 and 26 need an email address for your hosting account and its password:

$mail->Username = "EMAIL FROM HOST";
$mail->Password = "PASSWORD";
$mail->SMTPSecure = "ssl"; //TLS
$mail->Port = 465; //587

$mail->addAddress('SAME EMAIL');
$mail->setFrom($email);

If you have, let's say, godaddy as your hoster and your email is 'jeff@godaddy.com', then you would use that email and its corresponding password. contact.php would live on the hosters server, so it's contents would be protected, hopefully....

This method, SMTP, seems to be the only way I know of to recieve an email from someone with a yahoo account!

If you are going to zip and send files back, please remember to amend the above lines to remove your details!

Thanks for your ongoing help.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Aug 23 2019, 07:48 PM
Post #8


Programming Fanatic
********

Group: Members
Posts: 1,674
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: 159
Attached File  contact.php ( 914bytes ) Number of downloads: 154
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Aug 23 2019, 11:17 PM
Post #9


Programming Fanatic
********

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



Attached is another version of your contact form. This version combines both HTML and PHP into 1 file. Be sure to modify your PHPMailer stuff before you load the page. Once the page is loaded leave both fields blank and click Submit. You should see a error message below each field. Then enter valid data and click Submit. Let me know what happens.
Attached File  example.php ( 2.31k ) Number of downloads: 164
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
chome4
post Aug 24 2019, 03:32 AM
Post #10


Newbie
*

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



QUOTE(CharlesEF @ Aug 23 2019, 11:17 PM) *

Attached is another version of your contact form. This version combines both HTML and PHP into 1 file. Be sure to modify your PHPMailer stuff before you load the page. Once the page is loaded leave both fields blank and click Submit. You should see a error message below each field. Then enter valid data and click Submit. Let me know what happens.
Attached File  example.php ( 2.31k ) Number of downloads: 164



This one worked perfectly!

I separated the above into index.php and 'sendmail.php'. I used the php include to call the sendmail file before the form code.

I've incorporated it into the site I'm working on:

http://test.ericfinlayartist.co.uk/index.php

Apart from tidying things up, the only outstanding issue that has arisen is the page flying to the top after you click submit! I've had this before and will search through my history to find the solution.

Thanks for all your help.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Aug 24 2019, 11:49 AM
Post #11


Programming Fanatic
********

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



Glad you got it working. You could also spice things up with CSS.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Aug 25 2019, 02:14 PM
Post #12


Programming Fanatic
********

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



I looked over the example.php code again (something was nagging at me) and found a logic error. Attached is a corrected copy of the file.
Attached File  example.php ( 2.33k ) Number of downloads: 156
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
chome4
post Aug 27 2019, 11:10 AM
Post #13


Newbie
*

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



QUOTE(CharlesEF @ Aug 25 2019, 02:14 PM) *

I looked over the example.php code again (something was nagging at me) and found a logic error. Attached is a corrected copy of the file.
Attached File  example.php ( 2.33k ) Number of downloads: 156



I made the changes (if(!count($errors))), don't know what changes will take place, but I'll defer to your much much better knowledge!

During my research into the page scrolling to the top after the 'Submit' button is clicked, and found out it's pretty common. Trying my hand had looking into Javascript to prevent this from happening....

Thanks again...
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Aug 27 2019, 11:51 AM
Post #14


Programming Fanatic
********

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



The logic error was because I used an 'else' in the 2nd validation 'if' test. Instead of the 'else' I should have used the 'if(!count($errors))'. Left as it was the validation would fail if you typed in a message but left the e-mail blank. The correction will prevent this from happening.

Yes, after a submit the new page (or same page) will load at the top. And yes, AJAX can bypass this.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

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

 



- Lo-Fi Version Time is now: 12th November 2019 - 01:25 PM