I have started to make a message box, where a user can send a message to me. When I run the code the boxes is working fine, but I never receive a mail, and I do not get any errors. Can anybody see if I am doing something totally wrong?
<?php
//set validation error flag as false
$error = false;
//check if form is submitted
if (isset($_POST['submit']))
{
$name = trim($_POST['txt_name']);
$fromemail = trim($_POST['txt_email']);
$subject = trim($_POST['txt_subject']);
$message = trim($_POST['txt_msg']);
//name can contain only alpha characters and space
if (!preg_match("/^[a-zA-Z ]+$/",$name))
{
$error = true;
$name_error = "Please Enter Valid Name";
}
if(!filter_var($fromemail,FILTER_VALIDATE_EMAIL))
{
$error = true;
$fromemail_error = "Please Enter Valid Email ID";
}
if(empty($subject))
{
$error = true;
$subject_error = "Please Enter Your Subject";
}
if(empty($message))
{
$error = true;
$message_error = "Please Enter Your Message";
}
if (!$error)
{
//send mail
$toemail = "test#stackoverflow.com";
$subject = "Enquiry from Visitor " . $name;
$body = "Here goes your Message Details: \n\n Name: $name \n From: $fromemail \n Message: \n $message";
$headers = "From: $fromemail\n";
$headers .= "Reply-To: $fromemail";
if (mail ($toemail, $subject, $body, $headers))
$alertmsg = '<div class="alert alert-success text-center">Message sent successfully. We will get back to you shortly!</div>';
else
$alertmsg = '<div class="alert alert-danger text-center">There is error in sending mail. Please try again later.</div>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 3 Contact Form Example</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport" >
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 well">
<form role="form" class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contactform">
<fieldset>
<legend>Bootstrap Contact Form</legend>
<div class="form-group">
<div class="col-md-12">
<label for="txt_name" class="control-label">Name</label>
</div>
<div class="col-md-12">
<input class="form-control" name="txt_name" placeholder="Your Full Name" type="text" value="<?php if($error) echo $name; ?>" />
<span class="text-danger"><?php if (isset($name_error)) echo $name_error; ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="txt_email" class="control-label">Email ID</label>
</div>
<div class="col-md-12">
<input class="form-control" name="txt_email" placeholder="Your Email ID" type="text" value="<?php if($error) echo $fromemail; ?>" />
<span class="text-danger"><?php if (isset($fromemail_error)) echo $fromemail_error; ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="txt_subject" class="control-label">Subject</label>
</div>
<div class="col-md-12">
<input class="form-control" name="txt_subject" placeholder="Your Subject" type="text" value="<?php if($error) echo $subject; ?>" />
<span class="text-danger"><?php if (isset($subject_error)) echo $subject_error; ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="txt_msg" class="control-label">Message</label>
</div>
<div class="col-md-12">
<textarea class="form-control" name="txt_msg" rows="4" placeholder="Your Message"><?php if($error) echo $message; ?></textarea>
<span class="text-danger"><?php if (isset($message_error)) echo $message_error; ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input name="submit" type="submit" class="btn btn-primary" value="Send" />
</div>
</div>
</fieldset>
</form>
<?php if (isset($alertmsg)) { echo $alertmsg; } ?>
</div>
</div>
</div>
</body>
</html>
Related
I am stumped....
I have created a PHP form to gather some user information and email it to the site owner, after the form is submitted, the fields auto complete with a '1' I am assuming this means the field was true and submitted.
Does anyone have any suggestions on how to hide or remove the 1 after the form is submitted? I just want the form to be blank after submission. Here is the code, Sorry for the poor formatting and eye bleeding wall of code.
Thank you in advance for any help!
PHP Code
$errName = "";
$errcatBreed = "";
$errEmail = "";
$errMessage = "";
$result = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$catBreed = $_POST['catBreed'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'PearTreeHill Contact Form';
$to = 'test#domain.com';
$subject = "New furbaby enquiry from $name";
$body ="From: $name\n Cat Breed: $catBreed\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if cat breed has been entered
if (!$_POST['catBreed']) {
$errcatBreed = 'Please enter either Ragdoll or British ShortHair';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// RECAPTCHA
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
$secret = 'Private Key';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success)
{
$succMsg = 'Your contact request have submitted successfully.';
}
else
{
$errMsg = 'Robot verification failed, please try again.';
}
}
// If there are no errors, send the email
if (!$errName && !$errcatBreed && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
HTML Code
<!-- contact form -->
<section id="contactUs" class="bg">
<div class="container">
<div class="row">
<div class="col-lg-8 mx-auto">
<div class="text-center">
<h2 class="w3-tangerine">Contact Us</h2>
</div>
<!-- start of entry form -->
<form class="form-horizontal" role="form" method="post" action="index.php">
<!-- name entry -->
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars(isset($_POST['name'])); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<!-- cat breed selection -->
<div class="form-group">
<label for="catBreed" class="col-sm-2 control-label">Cat Breed</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="catBreed" name="catBreed" placeholder="'Ragdoll' or 'British ShortHair'" value="<?php echo htmlspecialchars(isset($_POST['catBreed'])); ?>">
<?php echo "<p class='text-danger'>$errcatBreed</p>";?>
</div>
</div>
<!-- email address entry -->
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-12">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars(isset($_POST['email'])); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<!-- Body of message -->
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-12">
<textarea class="form-control" rows="4" name="message" placeholder="Please enter any other information"><?php echo htmlspecialchars(isset($_POST['message']));?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<!-- reCAPTCHA -->
<div class="g-recaptcha" data-sitekey="Public Key" data-callback="recaptcha_callback"></div>
<!-- send button -->
<input disabled="disabled" id="submit" name="submit" type="submit" value="Send Message" class="btn btn-danger">
<label for="submit" class="col-sm-8 control-label">Please allow up to 48 hours for a response!</label>
</div>
</div>
<!-- entry alert -->
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
<?php echo htmlspecialchars(isset($_POST['email'])); ?>
Here you display the result of isset() (TRUE or FALSE).
You should do :
<?php if ( isset($_POST['email']) ) echo htmlspecialchars($_POST['email']); ?>
And this for each field :-)
change
echo htmlspecialchars(isset($_POST['name']));
to
echo htmlspecialchars ($_POST['name'])
I mean remove isset();
I'm new to PHP and I've got issue in my contact form. When I press "Submit" in my form it skips (somewhere, I don't know where and why it happens) to other site, and conditions are not checked by the php code. What's more, you can type wrong answer in "human recognizer" and it will still send and email.
I was looking for som bad declarations or wrong syntax, but all seems to be good. I assume that also my contact.php responds properly if it sends an email (but without checking conditions).
I don't know if it's connected but my modal window in it also doesn't want to close (but on the other site the same code works fine, when there is other form withoud "action=contact.php" field).
My main head.php:
<!--HEAD-->
<head>
<title>X</title>
<!--META-->
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="SitePoint">
<!--CSS-->
<link id="theme" rel="stylesheet" href="css/light.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Font Awesome -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<!--END OF HEAD-->
<body>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
<script src="js/dropdown.js"></script>
<script src="js/scrolling-nav.js"></script>
<script src="js/theme-switch.js"></script>
<script src="js/nav-position.js"></script>
<nav id="mainNav">
<bar>
<i id="hamburger" class="fa fa-bars" aria-hidden="true"></i>
</bar>
<ul id="menu">
<li>Main</li>
<li>Generator</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
<section id = "main" >
<div class = "content">
<h1>Hello!</h1>
<p>:)</p>
</div></section>
<section id = "generator">
<div class = "content">
<h1>Generator</h1>
<form id="generator-form" ="form-horizontal" role="form" method="post" action="generator.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="idCardNumber" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="idCardNumber" name="idCardNumber" placeholder="Student ID Card Number" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div></section>
<section id = "about">
<div class = "content">
<h1>About</h1>
<p></p>
</div></section>
<section id="contact">
<div class="content">
<h1>Contact</h1>
<p><a class="btn btn-default btn-lg" href="#contact-form">Contact Us</a></p>
<p><iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d5122.204450340393!2d19.91387757798398!3d50.065647167696376!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47165ba756b59b21%3A0xb20c8dba21b317d1!2sAkademia+G%C3%B3rniczo-Hutnicza+im.+Stanis%C5%82awa+Staszica+w+Krakowie!5e0!3m2!1spl!2spl!4v1511628584066" width="500rem" height="500rem" frameborder="0" style="border:0" allowfullscreen></iframe></p>
</div>
</section>
<footer>
<label class="switch">
<input type="checkbox" onchange=" switchTheme(this)">
<span class="slider"></span>
</label>
<p>Copyright©2017 for </p>
</footer>
<!--SIGN UP-->
<div id="contact-form" class="modal-window">
<div>
Close
<form action="contact.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<p class="text-danger"><?php echo $errName; ?></p>
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<p class="text-danger"><?php echo $errEmail; ?></p>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" rows="4" name="message" value="<?php echo htmlspecialchars($_POST['message']);?>"></textarea>
<p class="text-danger"><?php echo $errMessage; ?></p>
</div>
<div class="form-group">
<label for="human">1 + 1 = ?</label>
<input type="text" class="form-control" id="human" name="human" pattern=".{1,}" required title="At least 1 character required" placeholder="Your Answer">
<p class="text-danger"><?php echo $errHuman; ?></p>
</div>
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary btn-lg"></input>
<div class="form-group">
<?php echo $result; ?>
</div>
</form>
</div>
</div>
<!--END SIGN UP-->
<!--CONTACT FORM-->
<div id="contact-form" class="modal-window">
<a title="Close" class="modal-close">Close</a>
<form id="contactForm" role="form" method="post" action="contact.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" pattern=".{3,}" required title="At least 3 characters required" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" pattern=".{3,}" required title="At least 3 characters required" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" rows="4" pattern=".{3,}" required title="At least 3 characters required" name="message">
<?php echo htmlspecialchars($_POST['message']);?>
</textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
<div class="form-group">
<label for="human">1 + 1 = ?</label>
<input type="text" class="form-control" id="human" name="human" pattern=".{1,}" required title="At least 1 character required" placeholder="Your Answer">
<p class='text-danger'>$errHuman</p>
</div>
<input name="submit" type="submit" value="Send" class="btn btn-primary btn-lg">
<div class="form-group">
<?php echo $result; ?>
</div>
</form>
</div>
<!--CONTACT FORM-->
</body>
</html>
My contact.php code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Generator Contact';
$to = 'kamykx#gmail.com';
$subject = 'Message from AGH Generator Form';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
//CHECK NAME
if (!$name || empty($name)) {
$errName = 'Please enter your name';
}
//CHECK EMAIL
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($email)) {
$errEmail = 'Please enter a valid email address';
}
//CHECK MESSAGE
if (!$message || empty($message)) {
$errMessage = 'Please enter your message';
}
//CHECK IF USER IS NOT A BOT
if ($human !== 2 || $human !=2) {
$errHuman = 'Please... proof that you are not a bot :>';
}
//SEND THE EMAIL IF THERE ARE NO EXISTING ERRORS
if (!empty($errName) && !empty($errEmail) && !empty($errMessage) && !empty($errHuman)) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! We will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
header("Location: home.php");
}
}
?>
EDIT:
I've done improvements in my php code (there was logical problem in line error fields are empty: was -> if(!empty($errName)) but should be -> if(empty($errName)).
But I've still got and issue. I've compressed the code in order to stay on the same page after contact form submit, but when we click the "submit" button nothing appears (no errors are displayed), page only refreshes and open the form again. What's wrong now? NEW CODE:
<!DOCTYPE HTML>
<html lang="en">
<!--HEAD-->
<head>
<title>AGH Application for entry with ECTS deficit generator</title>
<!--META-->
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="SitePoint">
<meta name="Description" content="It is simple PDF generator for sing with lack of ECTS for another term" />
<meta name="Keywords" content="ECTS, deficit, deficyt, Poland, Cracow, generator, application, form, pdf, AGH, UST, Akademia, Górniczko, Hutnicza, University, S cience, Technology, Polska, Kraków, " />
<!--CSS-->
<link id="theme" rel="stylesheet" href="css/dark.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Font Awesome -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<!--END OF HEAD-->
<body>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
<script src="js/dropdown.js"></script>
<script src="js/scrolling-nav.js"></script>
<script src="js/theme-switch.js"></script>
<script src="js/nav-position.js"></script>
<nav id="mainNav">
<bar>
<i id="hamburger" class="fa fa-bars" aria-hidden="true"></i>
</bar>
<ul id="menu">
<li>Main</li>
<li>Generator</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
<section id = "main" >
<div class = "content">
<h1>Hello!</h1>
<p>Welocome to The AGH Application for entry with ECTS deficit generator website. We hope that you use it just for fun :)</p>
</div></section>
<section id = "generator">
<div class = "content">
<h1>Generator</h1>
</div></section>
<section id = "about">
<div class = "content">
<h1>About</h1>
<p>This webapge was created as a project for the Web Technologies. The main reason why it exists is that very common among Students is that they want to apply for entry on another term with ECTS deficit. This site will help students and AGH employees by generating PDF application. We hope that everything at AGH will be fast and growing in the future. We want to make our students life BETTER! </p>
</div></section>
<section id="contact">
<div class="content">
<h1>Contact</h1>
<p><a class="btn btn-default btn-lg" href="#contact-form">Contact Us</a></p>
<?php echo $result; ?>
<p><iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d5122.204450340393!2d19.91387757798398!3d50.065647167696376!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47165ba756b59b21%3A0xb20c8dba21b317d1!2sAkademia+G%C3%B3rniczo-Hutnicza+im.+Stanis%C5%82awa+Staszica+w+Krakowie!5e0!3m2!1spl!2spl!4v1511628584066" width="500rem" height="500rem" frameborder="0" style="border:0" allowfullscreen></iframe></p>
</div>
</section>
<footer>
<label class="switch">
<input type="checkbox" onchange=" switchTheme(this)">
<span class="slider"></span>
</label>
<p>Copyright©2017 Marcin Kamiński for AGH </p>
</footer>
<!--SIGN UP-->
<?php
if (isset($_POST["send"])) {
$name = $_POST['name']; //Getting variable from form
$email = $_POST['email']; //Getting variable from form
$message = $_POST['message']; //Getting variable from form
$human = intval($_POST['human']); //Getting variable from form
$from = 'Generator Contact'; //Set sender
$to = 'kamykx#gmail.com'; //Where to send an email
$subject = 'Message from AGH Generator Form'; //Set the subject of email
$errName = $errEmail = $errMessage = $errHuman = ''; //Values of errors
$body ="From: $name\n E-Mail: $email\n Message:\n $message"; //Body of email
//CHECK NAME
if (empty($name)) {
$errName = 'Please enter your name';
}
//CHECK EMAIL
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || empty($email)) {
$errEmail = 'Please enter a valid email address';
}
//CHECK MESSAGE
if (empty($message)) {
$errMessage = 'Please enter your message';
}
//CHECK IF USER IS NOT A BOT
if ($human !== 2 || $human !=2) {
$errHuman = 'Please... proof that you are not a bot :>';
}
//SEND THE EMAIL IF THERE ARE NO EXISTING ERRORS
if (empty($errName) && empty($errEmail) && empty($errMessage) && empty($errHuman)) {
if (mail($to, $subject, $body, $from)) {
$result = '<div class="alert alert-success">Thank You! We will be in touch</div>';
}
else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
<div id="contact-form" class="modal-window">
<div>
Close
<form id="contactForm" role="form" method="post">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<p class="text-danger"><?php echo $errName; ?></p>
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<p class="text-danger"><?php echo $errEmail; ?></p>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" rows="4" name="message" value="<?php echo htmlspecialchars($_POST['message']);?>"></textarea>
<p class="text-danger"><?php echo $errMessage; ?></p>
</div>
<div class="form-group">
<label for="human">1 + 1 = ?</label>
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<p class="text-danger"><?php echo $errHuman; ?></p>
</div>
<button id="send" name="send" type="submit" value="Send" class="btn btn-default btn-lg">Send</button>
</form>
</div>
</div>
<!--END SIGN UP-->
</body>
</html>
Your issue is in the logic of this line:
if (!empty($errName) && !empty($errEmail) && !empty($errMessage) && !empty($errHuman)) {
Basically this is saying if everything is wrong, send the email. The not empty check means that there was an error and the variable now holds the error string.
Instead, you just need to create the variables as an empty string and check if they are still empty:
$errName = $errEmail = $errMessage = $errHuman = '';
// CHECK NAME... etc... all the checks
if (empty($errName) && empty($errEmail) && empty($errMessage) && empty($errHuman)) {
However, you're not displaying the error to the user, and you're ending up with a bunch of loose variables. I recommend a slightly different approach using an array of errors...
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$errors = [];
//CHECK NAME
if (!$name || empty($name)) {
$errors['name'] = 'Please enter your name';
}
//CHECK EMAIL
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($email)) {
$errors['email'] = 'Please enter a valid email address';
}
//etc...
if (empty($errors)) {
//send email
} else {
$result = '<div class="alert alert-danger">Sorry there was an error sending your message:<br>';
foreach ($errors as $key => $error) {
$result .= $error . '<br>';
}
$result .= '</div>';
}
However, if you immediately call a header function after this, the user will never see the error or get a chance to fix it. You can use a query string to send errors back to head.php and display them.
(FYI, the action of a form sends all the POST data to that file, and if there is no action specified, then the form posts to itself.)
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I found a Bootstrap form on the internet and now I want to send it to my email.
I have the form online, so no issues with localhost etc. When I try send it I get a message 'Thank You! I will be in touch'. So it sends? But I don't receive the email in my mailbox. Anyone an idea? See code below.
ps. When I run the code local he got some errors, see image. I don't have those errors online, but maybe that is the reason why it doesn't send anything?
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'example#example.com';
$subject = 'Message from Contact Demo ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
<meta name="author" content="BootstrapBay.com">
<title>Bootstrap Contact Form With PHP Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="mail.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>
Use this modified code to get rid of the initial localhost errors.
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email']; //Sender's email
$message = $_POST['message'];
$human = intval($_POST['human']);
$to = "receivers#email.address";
$subject = "Message from Contact Demo";
$body = "From: $name\n";
$body .= "E-Mail: $email\n";
$body .= "Message:\n $message";
$header = "From:" . $email . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail($to, $subject, $body, $header)) {
$result = '<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
<meta name="author" content="BootstrapBay.com">
<title>Bootstrap Contact Form With PHP Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
<?php if (isset($errName)) echo "<p class='text-danger'>$errName</p>" ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
<?php if (isset($errEmail)) echo "<p class='text-danger'>$errEmail</p>" ?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"></textarea>
<?php if (isset($errMessage)) echo "<p class='text-danger'>$errMessage</p>" ?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php if (isset($errHuman)) echo "<p class='text-danger'>$errHuman</p>" ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php if (isset($result)) echo $result ?>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>
PHP isset() will determine if the variable is set and is not NULL. Initially the variables are NULL so it will omit showing you the errors.
Note: most of the time email will be delivered to your email address. check spam/junk folders to see the email. This happens due to unverified source of the email server.
Edit 1
Combining PHPMailer with mailgun platform will ensure you will get your emails to the destination address and possibly avoid going to spam/junk email folder.
Edit 2
I've update my answer with the recent modifications. Please have a look. Tested on a live server. received the contact demo email to my spam/junk folder on Outlook, Straightly to my inbox on Gmail.
Screens
Form submit Outlook Gmail
Hope you will have good luck!
if you want the code ready without change it you only need to change your php.ini file and change the line relate to the error reporting.
search for something like
error_reporting = E_ALL
and change to error_reporting = E_ALL & ~E_NOTICE
with this you hide the undefine issue
and then you need to configure your smtp provider in your php.ini too
I've been working on a website with a contact form. Everything is working fine except that the attachment isn't mailed. Here are the HTML and PHP files:
(And this is the first time I ask question in this website, I apologise for this terrible formatting, thanks in advance)
<? php
require_once 'phpmailer/PHPMailerAutoload.php';
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'])) {
$email_address = $_POST['email'];
}
if (isset($_POST['phone'])) {
$phone = $_POST['phone'];
}
if (isset($_POST['comment'])) {
$message = $_POST['comment'];
}
$body_message = '<p>You have a new message from your website contact form.</p> </br> </br> <p> Here are the deatils:</p> </br> </br> <p>Name: </p>'.$name.
'<p>Email: </p>'.$email_address.
'<p>Phone: </p> '.$phone.
'</br> <p>Message: </p> </br>'.$message;
$mail = new PHPMailer;
$mail - > IsSMTP();
$mail - > Host = 'mail.ronaldtest.com';
$mail - > Username = 'Ronald#ronaldtest.com';
$mail - > Password = '151588';
$mail - > Port = 587;
$mail - > addAddress('ronaldng1588#gmail.com');
$mail - > setFrom('ronald#ronaldtest.com', 'Brighten Management Auto Mailer');
$mail - > Subject = 'Website Contact Form: $name';
$mail - > Body = $body_message;
if (isset($_FILES['attachmentFile']) && $_FILES['attachmentFile']['error'] == UPLOAD_ERR_OK) {
$mail - > AddAttachment($_FILES['attachmentFile']['tmp_name'],
$_FILES['attachmentFile']['name']);
}
$mail - > IsHTML(true);
if ($_POST["submit"]) {
if (!$_POST['name']) {
$error = "<br />Please enter your name";
}
if (!$_POST['email']) {
$error. = "<br />Please enter your email address";
}
if (!$_POST['phone']) {
$error. = "<br />Please enter your phone number";
}
if (!$_POST['comment']) {
$error. = "<br />Please enter a comment";
}
if ($error) {
$result = '<div class="alert alert-danger" There were error(s)
in your form: '.$error.
'</div>';
} else {
if (!$mail - > Send()) {
$result = '<div class="alert alert-danger">Sorry, there was
an error sending your message. Please try again later.</div>';
} else {
$result = '<div class="alert alert-success">Thank you, <strong>'.$_POST['name'].
'</strong>! I will be in touch!</div';;
}
}
}
?>
<section id="contact">
<? php include ( 'email.php'); ?>
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="feature_header text-center">
<h3 class="feature_title">Keep In <b>touch</b></h3>
<h4 class="feature_sub">Contact us for future improvements</h4>
<div class="divider"></div>
<?php if (isset($result)){ echo $result; } ?>
</div>
</div>
<div class="row">
<div class="contact_full">
<form method="post">
<div class="col-md-6 left">
<div class="left_contact">
<div class="control-group">
<div class="form-level controls">
<input type="text" name="name" class="input-block" placeholder="Your Name" value="<?php
if (isset($_POST[" name "])){
echo $_POST['name']; }?>"/>
<span class="form-icon fa fa-user"></span>
</div>
</div>
<div class="control-group">
<div class="form-level controls">
<input type="email" name="email" class="input-block" placeholder="Your Email" value="<?php
if (isset($_POST[" email "])){
echo $_POST['email']; }?>" />
<span class="form-icon fa fa-envelope-o"></span>
</div>
</div>
<div class="control-group">
<div class="form-level controls">
<input type="phone" name="phone" class="input-block" placeholder="Your Phone Number" value="<?php
if (isset($_POST[" phone "])){
echo $_POST['phone']; }?>" />
<span class="form-icon fa fa-phone"></span>
</div>
</div>
</div>
</div>
<div class="col-md-6 right">
<div class="control-group">
<div class="form-level controls">
<input type="file" name="attachmentFile" id="attachmentFile" value="<?php echo $_FILES['attachmentFile']['tmp_name'] ?>" />
<span class="fa fa-file"></span>
</div>
</div>
<div class="control-group">
<div class="form-level controls">
<textarea class="textarea-block" name="comment" rows="4" value="<?php
if (isset($_POST[" comment "])){
echo $_POST['comment']; }?>"></textarea>
<span class="form-icon fa fa-pencil"></span>
</div>
</div>
</div>
<div class="col-md-12 text-center">
<input type="submit" name="submit" class="btn btn-main featured" value="Submit Now" />
</div>
</form>
</div>
</div>
</div>
</div>
</section>
for a form to upload a file it has to have enctype="multipart/form-data" in the form tag. so in your case:
<form method="post" enctype="multipart/form-data">
I am using the code below to send an email and attach an image with a short description.
I am getting the error below and I don't understand why:
Notice: Undefined index: name in C:\wamp\www\form\index.php on line 64 Call Stack #TimeMemoryFunctionLocation 10.0012146120{main}( )..\index.php:0 ">**
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'example#domain.com';
$subject = 'Message from Contact Demo ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">
Thank You! I will be in touch
</div>
'; } else { $result='
<div class="alert alert-danger">
Sorry there was an error sending your message. Please try again later.
</div>
'; } } } ?> <!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
<meta name="author" content="BootstrapBay.com">
<title>Bootstrap Contact Form With PHP Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_post['name']); ?>"> <?php echo "<p class='text-danger'>
$errName
</p>
";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_post['email']); ?>"> <?php echo "<p class='text-danger'>
$errEmail
</p>
";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?>
</textarea>
<?php echo "<p class='text-danger'>
$errMessage
</p>
";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>
$errHuman
</p>
";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" type="submit" name="submit" value="submit" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
The error message explains exactly what the issue is. The name variable is not set when you first load the page, because nothing has been posted yet. So of course, $_POST['name'] does not exist. That's what the php error is telling you. You'll need to replace all of your $_POST inside the form with this:
<?php if (isset($_POST['name'])){ echo htmlspecialchars($_POST['name']); } ?> so you won't see those errors anymore. Also, you should be using $_POST, not $_post.
Alternatively, you can put error_reporting(0); at the top of your file and it will hide all of these warnings. However, it's not reccomended because if there is a real error you won't know what it is until you remove that line.