PHP mailer form data verification isn't working - php

NOTE: the mail function itself works fine, I have a hosted site with mail configured professionally. Mail is sent via this script, but my simple empty field handler never catches empty fields. I'm sure this is something super obvious, but I'm very new to php and the logic of my script seems sound. The code follows:
edit: I've left the form call within the php script in case someone finds issue with the bootstrap form itself. The redirect is temporary until I can get better form error handling going.
<body>
<!-- jQuery and bootstrap -->
<script src="https://code.jquery.com/jquery.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/counter.js"></script>
<div class="jumbotron-fluid" id="primary">
<?php
if (isset($_POST['submit'])) {
$from = 'Web_Form_Contact';
$to = 'example#example.com';
$subject = 'Job_Contact';
$c_type = $_POST["c_type"];
if (empty($_POST["c_name"])) {
$errName = 'Info missing: Name';
} else {
$c_name = $_POST["c_name"];
}
if (empty($_POST["c_email"] )) {
$errEmail = 'Please enter a valid email address';
} else {
$c_email = $_POST["c_email"];
}
if (empty($_POST["co_name"])) {
$errCoName = 'Info missing: Company Name';
} else {
$co_name = $_POST["co_name"];
}
if (empty($_POST["j_desc"])) {
$errjDesc = 'Info missing: Job Description';
} else {
$j_desc = $_POST["j_desc"];
}
$body = "From: $c_name\n E-Mail: $c_email\n Type: $c_type\n CoName: $co_name\n Description\n $j_desc";
if (empty($errName && $errEmail && $errCoName && $errjDesc)) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch\n This page will redirect in 5 seconds.</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
} else {
echo "$errName\n $errEmail\n $errCoName\n $errjDesc";
}
}
echo "$result";
?>
<div class="container">
<form class="form-horizontal" role="form" method="post" action="php/info.php"
<div class="row">
<div class="col-xs-10 col-sm-6 col-md-6 col-lg-offset-2 col-md-offset-2">
<<h2>Contact Me! <small></small></h2>
<form>
<div class="form-group row">
<label for="clientEmail" class="col-sm-2 form-control-label">Email</label>
<div class="col-sm-10">
<input type="email" name="c_email"class="form-control" id="clientEmail" placeholder="Email" >
</div>
</div>
<div class="form-group row">
<label for="clientName" class="col-sm-2 form-control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="c_name" id="clientName" placeholder="Your Name" >
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">Contact Type</label>
<div class="col-sm-10">
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios1" value="recruiter" checked>
I am a recruiter, and do not work directly for the company hiring.
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios2" value="employee">
I work for the company in question, and wish to discuss an open position.
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios3" value="other" >
Other
</label>
</div>
</div>
</div>
<div class="form-group row">
<label for="coName" class="col-sm-2 form-control-label">Company Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="co_name" id="coName" placeholder="Company Name" >
</div>
</div>
<!-- Job Description Text Box -->
<div class="form-group row">
<label for="Job Description ">Job Description: (1100 character max!)</label>
<div class="col-sm-10 col-lg-offset-2 col-md-offset-2 col-xl-offset-2 col-sm-offset-2" >
<div class="form-group">
<textarea class="form-control" rows="5" name="j_desc" id="jobDesc" placeholder="Job Description" ></textarea>
<p class="counter">1100</p>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="send" name="submit" value="Send"class="btn btn-secondary">Submit</button>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<!--redirect script -->
<script>
function redirect(page) {
setTimeout(function () {
window.location.href = page;
}, 5000);
}
//sends to homepage
redirect("../index.html");
</script>

You need to have an empty check for each variable:
if (empty($errName) && empty($errEmail) && empty($errCoName) && empty($errjDesc)) {
You can also set an email flag at the beginning to true, and if you hit any of the errors, you can set it to false. Then when you're ready to email, check to see if that email flag is still true.

Related

PHP - html form sent to gmail account

I never used PHP before, but I need to set up an HTML form from to be sent from my website to my gmail account. Checked some tutorials and came up with this code. But for some reason it is not working. When I hit submit it goes to a "Page Not Found
Looks like you've followed a broken link or entered a URL that doesn't exist on this site." Not sure what am I doing wrong.
I have my HTML document, and I have my PHP document. Saved together.
<form action="contact.php" method="POST" name="form">
<div class="row">
<div class="col-12 col-md-6">
<div class="contact-form">
<label for="name">Please enter your name:</label>
<input name="name" type="text" class="form-control" placeholder="Harry Potter">
</div>
</div>
<div class="col-12 col-md-6">
<div class="contact-form">
<label for="mail">Your email address:</label>
<input name="mail" type="email" class="form-control" placeholder="harry#potter.com">
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="contact-form">
<label for="message">How can I help you?</label>
<textarea name="message" class="form-control" id="form-text" cols="30" rows="10" placeholder="Let's do some magic"></textarea>
</div>
</div>
</div>
<div id="submit" class="text-center">
<button name="submit" type="submit" class="btn mt-5 mx-auto">Submit</button>
</div>
</form>
<?php
if(isset($_POST["submit"])) {
$name=$_POST["name"];
$mailFrom=$_POST["mail"];
$message=$_POST["message"];
$mailTo = "myaddress#gmail.com";
$subject = "Form from my website";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name."\n\n".$message;
if(mail($mailTo, $subject, $txt, $headers)) {
echo "<h1>I recieved your email! Will be in touch with you soon.</h1>";
}
else{
echo "<h1>Something went wrong! Try again.</h1>";
}
header("Location: https://www.whatever.com");
}
?>

HTML/PHP Email forms not functioning as wanted

I am doing a contact us form into my website, and i am now stuck.
Here's the PHP code:
#Receive user input
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
$palvelu = $_POST['palvelu'];
#Filter user input
function filter_email_header($form_field) {
return preg_replace('/[nr|!/<>^$%*&]+/','',$form_field);
}
$Email = filter_email_header($Email);
#Send email
$headers = "From: $Email\r\n";
$sent = mail('user#example.com', 'Yhteydenotto Pyyntö: ', $Message, $headers);
#Thank user or notify them of a problem
if ($sent) {
?><html>
<head>
<title>Kiitos yhteydenotosta!</title>
</head>
<body>
<h1>Kiitos yhteydenotosta!</h1>
<p>Olemme sinuun yhteydessä mahdollisimman nopeasti!</p>
</body>
</html>
<?php
} else {
?><html>
<head>
<title>Jokin meni vikaan!</title>
</head>
<body>
<h1>Jokin meni vikaan!</h1>
<p>Emme pystyneet lähettämään viestiä?</p>
</body>
</html>
<?php
}
?>
And here's the html code of the form:
<div class="col-lg-5 d-flex align-items-stretch">
<div class="bg-white">
<div class="w-100 heading-title bg-primary text-center">
<h2 class="mb-0">Ota meihin yhteyttä!</h2>
</div>
<form action="email.php" method="post" class="appointment bg-white p-4 p-md-5">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<div class="form-field">
<div class="select-wrap">
<div class="icon"><span class="fa fa-chevron-down"></span></div>
<select name="palvelu" id="" class="form-control">
<option value="">Valitse palvelu</option>
<option value="">Sammaleen poisto</option>
<option value="">Katon pinnoitus</option>
<option value="">Tiilikaton huolto</option>
<option value="">Vuosihuolto</option>
</select>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" id="Name" name="Name" placeholder="Nimi" class="form-control">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="Email" id="Email" name="Email" placeholder="Sähköposti" class="form-control">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="input-wrap">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="input-wrap">
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<textarea id="Message" name="Message" class="form-control" placeholder="Viesti" rows="6" maxlength="3000"></textarea>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="submit" value="Lähetä viesti" class="btn btn-primary py-3 px-4">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
So that's in finnish but i have this list where you can select what job you want to discuss "palvelut" and then the basic form. What it should do is to send an email with "who sent it" "the job they want" and the basic form stuff "name" "email" and the "message". But now it only seems to send an blank email with only the "yhteydenotto pyyntö" it means like "a new question". Could someone help me? What am i doing wrong...
Solution Here!!!
* Your filter_email_header is contain Problem so we use Default FILTER_SANITIZE_EMAIL filter.
Remove code
#Filter user input
function filter_email_header($form_field) {
return preg_replace('/[nr|!/<>^$%*&]+/','',$form_field);
}
$Email = filter_email_header($Email);
Replace the Fix
// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
*Your Form select option not present value so whatare the value select empty value to shown email form
<select name="palvelu" id="palvelu" class="form-control">
<option value="Valitse palvelu">Valitse palvelu</option>
<option value="Sammaleen poisto">Sammaleen poisto</option>
<option value="Katon pinnoitus">Katon pinnoitus</option>
<option value="Tiilikaton huolto">Tiilikaton huolto</option>
<option value="Vuosihuolto">Vuosihuolto</option>
</select>

Contact form not working in wordpress

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

PHP form submit with Bootstrap togglable tab feature

Hy everybody,
this is the website I'm building using the Bootstrap togglable tab feature to handle the content I want to show (on the right side).
My problem comes when I want to submit a contact form with php because the eventual errors are correctly shown in the Contact page but the form automatically redirects to the home page since there is no proper link to the contact page itself. The question might be silly but I cannot find a way to be redirected on the same Contact page when I submit the form. Any help?
Here is the HTML code:
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<h3>MARCELLO GIOVENCO PHOTOGRAPHY</h3>
</div>
</div>
<!-- Menu -->
<div class="container-fluid">
<div class="row">
<div class="col-sm-3">
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav nav-pills nav-stacked">
<li class="dropdown">
PROJECTS
<ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
<li>NAPOLI</li>
<li>PORTRAITS</li>
</ul>
</li>
<li>BIO</li>
<li>CONTACT</li>
<li>BLABLABLA</li>
</ul>
</div>
</div>
<!-- Content -->
<div class="col-sm-9">
<div id="myNavbar" class="tab-content">
<div class="tab-pane fade active in" id="home">
<p>home</p>
</div>
<div class="tab-pane fade" id="napoli">
<p>napoli</p>
</div>
<div class="tab-pane fade" id="portraits">
<p>portraits</p>
</div>
<div class="tab-pane fade" id="bio">
<p>bio</p>
</div>
<div class="tab-pane fade" id="contact">
<?php include('contact.php');?>
</div>
</div>
</div>
</div>
</div>
and this is the PHP code for contact.php
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'www.marcellogiovenco.com';
$to = 'marcellomariagiovenco#gmail.com';
$subject = "$name sent you a message!";
$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';
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$errName = "Only letters and white space allowed";
}
}
// 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 for the message! 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>';
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form id="myForm" class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-5">
<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 class="col-sm-5"></div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-5">
<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 class="col-sm-5"></div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-5">
<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
<div class="col-sm-5"></div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
<div class="col-sm-5"></div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-2">
<p>All fields are required.</p>
</div>
</div>
<div class="form-group">
<div class="col-sm- 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-5 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
From what I understand , your problem is that when you click on the "submit " button is redirected to the index or to the first tab . As I ran until I got this script that redirects me to the tab where the submit button that I clicked is .
$(document).ready(function() {
if(location.hash) {
$('a[href=' + location.hash + ']').tab('show');
}
$(document.body).on("click", "a[data-toggle]", function(event) {
location.hash = this.getAttribute("href");
});
});
$(window).on('popstate', function() {
var anchor = location.hash || $("a[data-toggle=tab]").first().attr("href");
$('a[href=' + anchor + ']').tab('show');
});
Hope that it can help you.
Use contact.php as action for the form. Then, at the end of the if, redirect the user to a confirmation page or to the form (in case of errors) using the header() function.
You can use jQuery to submit your form with ajax.
http://api.jquery.com/jquery.ajax/
$('#myForm').on('submit', function() {
$.ajax({
url: 'some-url',
type: 'post',
dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
// Show success message
}
});
});

How do I enable SMTP for my PHP contact form?

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

Categories