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.
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.
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.
example.php ( 874bytes )
Number of downloads: 430
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)
contact.zip ( 51.15k )
Number of downloads: 314
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.
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.
test.php ( 643bytes )
Number of downloads: 415
contact.php ( 914bytes )
Number of downloads: 398
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.
example.php ( 2.31k )
Number of downloads: 398
Glad you got it working. You could also spice things up with CSS.
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.
example.php ( 2.33k )
Number of downloads: 406
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.
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)