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">
Related
so I'm trying to make the contact form using PHP with validations. So when the input fields are not filled and I click send button it displays the error that says to fill in all blanks but when I fill in everything and click on send button, it sends message to my email address but doesn't display that the message was sent successfully, it just says "page not found" here's my code for contact.php and send.php
<div class="container">
<div class="row text-center">
<div class="col-12 heading">
<h1>Contact Us</h1>
</div>
<div class="col-12">
<?php
$Msg = "";
if(isset($_GET['error'])) {
$Msg = "Please Fill in All Blannks";
echo '<div class="alert alert-danger">'.$Msg.'</div>';
}
if(isset($_GET['success'])) {
$Msg = "Your Message Has Been Sent";
echo '<div class="alert alert-success">'.$Msg.'</div>';
}
?>
</div>
</div>
</div>
<div class="container">
<div class="row text-center">
<div class="col-12">
<form class="reportform" action="send.php" method="post">
<input type="text" name="UName" placeholder="Full Name"><br>
<input type="email" name="Email" placeholder="Your e-mail"><br>
<input type="text" name="Subject" placeholder="Subject"><br>
<textarea name="msg" placeholder="Message"></textarea><br>
<h6>By clicking send button, you agree our <a target="_blank" href="../credits/ppolicy.html">Privacy Policy</a></h6>
<button type="submit" name="btn-send" class="btn btn-primary sendbtn">Send</button>
</form>
</div>
</div>
</div>
<?php
if(isset($_POST['btn-send'])){
$UserName = $_POST['UName'];
$Email = $_POST['Email'];
$Subject = $_POST['Subject'];
$Msg = $_POST['msg'];
if(empty($UserName) || empty($Email) || empty($Subject) || empty($Msg)) {
header('location: contact.php?error');
}
else {
$to = "nika.makhatadze17#gmail.com";
if(mail($to,$Subject,$Msg,$Email)) {
header("location: index.php?success");
}
}
}
else {
header("location: contact.php");
}
?>
By Success you redirect to index.php?success but you defined the Success case in your contact.php, so i would say you just have to change header("location: index.php?success"); to header("location: contact.php?success");
Hey try this code we mead some changes on your code
pass success msg value on header location
<div class="container">
<div class="row text-center">
<div class="col-12 heading">
<h1>Contact Us</h1>
</div>
<div class="col-12">
<?php
$Msg = "";
if(isset($_GET['error'])) {
$Msg = "Please Fill in All Blannks";
echo '<div class="alert alert-danger">'.$Msg.'</div>';
}
if(isset($_GET['success'])) {
$Msg = "Your Message Has Been Sent";
echo '<div class="alert alert-success">'.$Msg.'</div>';
}
?>
</div>
</div>
</div>
<div class="container">
<div class="row text-center">
<div class="col-12">
<form class="reportform" action="send.php" method="post">
<input type="text" name="UName" placeholder="Full Name"><br>
<input type="email" name="Email" placeholder="Your e-mail"><br>
<input type="text" name="Subject" placeholder="Subject"><br>
<textarea name="msg" placeholder="Message"></textarea><br>
<h6>By clicking send button, you agree our <a target="_blank" href="../credits/ppolicy.html">Privacy Policy</a></h6>
<button type="submit" name="btn-send" class="btn btn-primary sendbtn">Send</button>
</form>
</div>
</div>
</div>
<?php
if(isset($_POST['btn-send'])){
$UserName = $_POST['UName'];
$Email = $_POST['Email'];
$Subject = $_POST['Subject'];
$Msg = $_POST['msg'];
$successMsg = 1;
if(empty($UserName) || empty($Email) || empty($Subject) || empty($Msg)) {
header('location: contact.php?error');
}
else {
$to = "nika.makhatadze17#gmail.com";
if(mail($to,$Subject,$Msg,$Email)) {
header("location: index.php?success=".$successMsg);
}
}
}
else {
header("location: contact.php");
}
I have written a script mail.php to handle the validation of form data and to send the form data via email. I have been testing this script by entering known invalid data however the script does not echo the error messages and just sends the data anyway.
Here is the form html:
<form method="post" action="mail.php" class="col-12" id="form-book">
<div class="row">
<div class=" form-group col-12">
<p>Fill out the form below to tell me about your problem. <strong>Any problems to do with technology, all the solutions.</strong> Just tell me what you can, however the more info you give the better.</p>
</div>
<div class="form-group col-6">
<label></label>
<input id="name" name="name" placeholder="Name" type="text" required="required" class="form-control here bottom"> <span class="error"> <?php echo $nameErr; echo $nameErrEmpty;?></span>
</div>
<div class="form-group col-6">
<label></label>
<input id="phone" name="phone" placeholder="Phone#" type="text" required="required" class="form-control here bottom"> <span class="error"> <?php echo $phoneErr; echo$phoneErrEmpty;?></span>
</div>
<div class="form-group col-12">
<label></label>
<input id="email" name="email" placeholder="Email (Optional)" type="text" class="form-control here bottom"> <span class="error"> <?php echo $emailErr; echo $emailErrEmpty;?></span>
</div>
<div class="form-group col-6">
<input data-provide="datepicker" id="date" name="date" placeholder="Pick a date" required="required" class="form-control here bottom"> <span class="error"> <?php echo $dateErr; echo $dateErrEmpty;?></span>
</div>
<div class="form-group col-6">
<input type="text" id="time" name="time" placeholder="Choose the best time" required="required" class="form-control here bottom timepicker"> <span class="error"> <?php echo $timeErr; echo $timeErrEmpty;?></span>
</div>
<div class="form-group col-12">
<label></label>
<textarea id="message" name="message" cols="40" rows="5" class="form-control" required="required" aria-describedby="messageHelpBlock"></textarea> <span class="error"> <?php echo $messageErr; echo $messageErrEmpty;?></span>
<span id="messageHelpBlock" class="form-text text-muted">Tell me a little about whats going on or what you need help with.</span>
</div>
<div class="form-group col-12">
<button name="submit" type="submit" class="btn btn-primary">Send your message</button>
</div>
</div>
<!--row-->
</form>
Here is mail.php:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
date_default_timezone_set("Pacific/Honolulu");
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$date = $_POST['date'];
$time = $_POST['time'];
$message = $_POST['message'];
$subject = "Appointment Booked";
$mailTo = "itguy#johnpuaoi.com";
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (empty($name)) {
$nameErrEmpty = "Your name is required";
} elseif (is_int($name)) {
$nameErr = "That is not a valid name";
} else {
$nameTested = test_input($name);
}
if (empty($phone)) {
$phoneErrEmpty = "Your phone number is required";
} elseif (is_int($name)) {
$phoneTested = test_input($phone);
} else {
$phoneErr = "Phone Number needs to be in this format e.x 1234567890";
}
if (empty($email)) {
$emailErrEmpty = "Your name is required";
} elseif (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "That is not a valid email";
} else {
$emailTested = test_input($name);
}
if (empty($message)) {
$messageErrEmpty ="You must tell me about your problem";
} else {
$messageTested = test_input($message);
}
$dateTested = test_input($date);
$timeTested = test_input($time);
$headers = "From: ".$emailTested;
$txt = "An appointment was booked by ".$nameTested.".\n\n".$dateTested." #".$timeTested.".\n\n".$messageTested;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.php?mailsend");
}
?>
I have searched stack for similar issues but have found none. I am a total noob to php so any help would be appreciated. Thanks!
I am trying to create a contact form in php. But the problem I am facing is that when ever I try to submit the form it shows 404 exception rather than the actual file which has the contact form. The theme is custom made. I have added my code below
<?php
if(isset($_POST)) {
if(trim($_POST["name"]) === "") {
$nameError = "Please enter your name.";
$hasError = true;
} else {
$name = trim($_POST["name"]);
}
if(trim($_POST["email"]) === "") {
$emailError = "Please enter your email address.";
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST["email"]))) {
$emailError = "You entered an invalid email address.";
$hasError = true;
} else {
$email = trim($_POST["email"]);
}
if(trim($_POST["phone"]) === "") {
$phoneError = "Please enter your phone address.";
$hasError = true;
} else {
$phone = trim($_POST["phone"]);
}
if(trim($_POST["comments"]) ==="") {
$commentError = "Please enter a message.";
$hasError = true;
} else {
if(function_exists("stripslashes")) {
$comments = stripslashes(trim($_POST["comments"]));
} else {
$comments = trim($_POST["comments"]);
}
}
if(!isset($hasError)) {
$emailTo = get_option("tz_email");
if (!isset($emailTo) || ($emailTo == "") ){
$emailTo = get_option("admin_email");
}
$subject = "[PHP Snippets] From ".$name;
$body = "Name: $name \n\nEmail: $email \n\nPhone:$phone\n\nComments: $comments";
$headers = "From: ".$name." <".$emailTo.">" . "\r\n" . "Reply-To: " . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php get_header(); ?>
<div class="row" style="margin-top:150px;">
<div class="bg-container"></div>
<div class="col-md-4">
<div class="card contact-form-card">
<form action="<?php esc_url(the_permalink());?>" method="post">
<fieldset>
<legend>Give us your details</legend>
<div class="form-group label-floating">
<label class="control-label" for="user-name">Enter your full name</label>
<input class="form-control" id="user-name" name="name" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="user-email">Enter your email id</label>
<input class="form-control" id="user-email" name="email" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="user-phone">Enter your phone number</label>
<input class="form-control" id="user-phone" name="phone" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="commenets">Comments</label>
<textarea class="form-control" id="commenets" name="comments" type="text"></textarea>
</div>
<button type="submit" name="submit" class="btn btn-primary btn-raised col-xs-12">Submit</button>
</fieldset>
</form>
</div>
</div>
<div class="col-md-8">
<h1 class="text-center">We are currently maintaining over 1000 properties in north Bangalore</h1>
<div class="owl-carousel">
<div class="item card">
<p class="statement">"Poorna is an immaculate real estate consulting professional, and possess several unique skills up his sleeves like property consulting report writing that are rare to find in a professional framework."</p>
<p class="by">Shariq Saleem, Director, Armchair Solutions India</p>
</div>
<div class="item card">
<p class="statement">"Square Capital Assets understands your needs clearly and they are very easy to work with. Once they taken over managing my property, it was a smooth ride"</p>
<p class="by">Manoj</p>
</div>
</div>
</div>
</div>
<div class="row" style="margin-top:15px;">
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/sign_the_agreement.png";?> alt="Sign the agreement">
<h5>Sign with us</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/choose_your_tenants.png";?> alt="Choose your tenants">
<h5>We will find reliable tenants.</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/collect_security_deposit.png";?> alt="Collect Security Deposit">
<h5>Rental Agreement</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/rent_assured.png";?> alt="Rent Assured">
<h5>Collecting rent and keeping your home safe</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/repairs_under_2000.png";?> alt="Repairs Under Rs.2,000">
<h5>Regular check up and maintenance</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/property_tax.png";?> alt="Property Taxes?">
<h5>Managing taxes and bills</h5>
</div>
</div>
</div>
<?php get_footer(); ?>
Just change the fields name like below
<input class="form-control" id="user-name" name="Fname" type="text">
<input class="form-control" id="user-email" name="Femail" type="text">
You will not get 404 error
:)
Naming a field in the form "name" caused that problem.
I renamed it to customer name and it started working
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>
I have successfully set up a php script for my contact form on my website but I have recently found out that my server provider does not accept php. Instead I have use SMTP.
Can anyone help me as this is new to me. I've attempted using other scripts but I cannot implement it.
This is my php code:
<?php
/* Set e-mail recipient */
$myemail = "mail#louisreed.co.uk";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Your Name");
$email = check_input($_POST['email'], "Your E-mail Address");
$subject = check_input($_POST['subject'], "Message Subject");
$message = check_input($_POST['message'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://louisreed.co.uk');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
My HTML form:
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form role="form" action="http://www.louisreed.co.uk/includes/mailer.php" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
</div>
</div>
Any suggestions welcome!
Thanks! :)
Louis
Oke I checked a few thinks. I guess you could atleast try the following. I know your host doesn't support php which find hard to believe tough. So maybe something else is wrong. What is the host you are using?
<?php
if($_POST) {
//Strip user input
function sanitize($value) {
//Escape the user input and then strip all html tags
//mysql_* is not recommended but I guess you don't store data in a database
$value = mysql_real_escape_string(strip_tags($value));
//Return the sanitized user input
return $value;
}
//To display all errors
function errors($error) {
echo '<ul class="error">';
foreach($error as $fail) {
echo '<li>'.$fail.'</li>';
}
echo '</ul>';
}
//All the input fields
$name = sanitize($_POST['name']);
$email = sanitize($_POST['email']);
$subject = sanitize($_POST['subject']);
$message = sanitize($_POST['message']);
//Check if there are no empty fields
if(!empty($name), !empty($email) && !empty($subject) && !empty($message)) {
if(strlen($name) <= 3) {
$error[] = 'Name '.$name.' is too short';
}
if(strlen($email) <= 3) {
$error[] = 'Email '.$email.' is too short';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error[] = 'Email '.$email.' is not a valid email';
}
if(strlen($subject) <= 3) {
$error[] = 'Subject '.$subject.' is too short';
}
if(strlen($message) <= 8) {
$error[] = 'Message is too short';
}
//If there are no errors
if(empty($error)) {
$to = "mail#louisreed.co.uk"; //The email you want to send it to
//Subject already set
$headers = "FROM: My site"; //Sets the headers
//The message for the email
$message = "Someone has sent you a message using your contact form:\r\n\r\nName: ".$name."\r\nEmail: ".$email."\r\nSubject: ".$subject."\r\n\r\nMessage: ".$message;
$mail = mail($to, $subject, $message, $headers);
//If the mail has been send
if($mail) {
header('Location: http://louisreed.co.uk/index.php?success');
exit();
}
}
} else {
$error[] = 'There are empty fields';
}
//If there are errors show them
if(!empty($error)) {
echo errors($error);
}
}
//If issset success (from the header()) display a message
if(isset($_GET['success'])) {
echo '<p>Thank you for your message</p>';
}
?>
<form role="form" action="" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
So try the exact code I gave you above just copy and past it over the form code you have now. You can leave the rest of your code as is. After that save the complete file as index.php. After that remove the index.html file from your FTP. If you have done that you can upload the index.php file now check what you get. If there are still some errors please let me know