A simple php contact form with Bootstrap
Who knew it was that difficult to find a simple and decent form with php code online? It took me several hours to eventually found a working solution. So if you are out there, frustrated and sad, here’s your solace.
The present form is a combination of a simple html + php code from the freecontactform.com website and a simple (yet beautiful) contact form designed with Bootstrap.
You can find the whole code here. But for those too lazy to open a link, here’s how it looks like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<title>My form</title>
</head>
<body>
<div col-md-6 "img-thumbnail">
<div id="feedback"> <div class="container">
<div class="col-md-6 col-md-offset-3">
<div class="form-area">
<form name="contactform" method="post" action="thank_you.php">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact Form</h3>
<div class="form-group">
<input type="text" class="form-control" name="first_name" placeholder="First Name" required>
</div>
<div class="form-group">
<input type="text" class="form-control" name="last_name" placeholder="Last Name" required>
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" class="form-control" name="telephone" placeholder="Telephone" required>
</div>
<div class="form-group">
<textarea class="form-control" type="textarea" name="comments" placeholder="Message" maxlength="140" rows="7"></textarea>
<span class="help-block"><p id="characterLeft" class="help-block ">You have reached the limit</p></span>
</div>
<button type="submit" value="submit" class="btn btn-primary pull-right">Submit Form</button>
</form>
</div>
</div>
</div> </div> <!--feedback-->
<body>
</html>
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_subject = "Your email subject line";
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
if(strlen($error_message) > 0) {
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
In your html file, the name of your “action” in the form must be the name of your php file (here: thank_you.php) which will also be the name of your confirmation page
Don’t forget to change your email address in the PHP file under “// EDIT THE 2 LINES BELOW AS REQUIRED”
You can insert custom html to appear on your thank you page at the bottom of your php file