Reputation: 27
So I have a problem, I uploaded my index.php file that is a contact form to my website server and when I fill it out and click submit it just refreshes my browser and no error occurs and no message shows that it failed or it was submitted. Here is my html code: https://codepen.io/themandelaeffect/pen/poJPBdZ?editors=1000 and this is the php code that I'm using:
<?php
ini_set('display_errors', true);
error_reporting(1);
if(isset($_POST['submit']))
{
ini_set('display_errors', true);
require_once "assets/PHPMailer-5.2-stable/PHPMailer-5.2-stable/PHPMailerAutoload.php";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$Comments = $_POST['message'];
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "host";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "username";
$mail->Password = "********";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 465;
$mail->From = "[email protected]";
$mail->FromName = "Contact";
$mail->addAddress($email, $name);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $Comments;
$mail->AltBody = $Comments;
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
}
PHP is new to me and I am not sure what to do. I copied this code from another stack overflow question because they said their code works because the original code that I used gave me lots of errors. Can someone help me please?
Upvotes: 0
Views: 135
Reputation: 420
i saw your code there i found you dont have any action page so if your php code in in other page just add page name to
<form action="your_page_name.php" method="post" class="signin-form mt-lg-5 mt-4">
and if your php code (above code you provided ) is in same page then no need to action , so your form starting simply would be like
<form method="post" class="signin-form mt-lg-5 mt-4">
so this was not exactly your problem , i just seen you mentioned you new in php so i just explained because your action was a bit wrong , you used action:"" insted of action="" well i just found your submit button have no type mentioned and also no any name , means you submitting your php code with name submit "if(isset($_POST['submit'])) " you you must define name for button and to submit a form with php you must need to define button type="submit" but you just added button like
<button class="btn submit">Submit</button>
so it will by default only refresh page so change it with
<button type="submit" name="submit" class="btn submit">Submit</button>
then it will work fine it will hit your php code and it will execute.
Upvotes: 1
Reputation: 37730
First of all, you're running an old version of PHPMailer; upgrade.
You don't show your form, but if it doesn't contain a named input element called submit
, your mail code will never run.
You are combining port 465 with tls
encryption mode; that won't work - do what the examples provided with PHPMailer recommend, as other combinations won't work.
Your Host
value should be your outgoing mail server, and any username and password should match that. If you have your own mail server, point at that, if not, use whatever your hosting provider recommends - we can't tell from out here.
Upvotes: 0