A Simple PHP Contact Form

Looking for something a bit more clever… See how to build an AJAX contact form here

Lets face it, the days of displaying an email address on your website for people to click on and contact you are over. Spambots and general menaces have ruined this for everyone, so it’s now all about contact forms.

When searching the internet for contact forms you are bombarded by many different types and designs using different coding and methods. Today we are going to run through how to make a simple PHP contact form that is safe and easy to implement and add to.

The first thing to think about is whether or not your webserver supports PHP, because if not, then this tutorial is not for you! Now, you will need to create a new PHP file, for this example we will call it contact.php.

Within the head section of your PHP document you need to add the variables that are going to read the form data and then output it to your email. So this is an example of what your head section should look like:


<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Contact Us</title>
<?php
$to = 'email@example.com';

$name = stripslashes($_POST['name']);
$number = stripslashes($_POST['number']);
$email = stripslashes($_POST['email']);
$comment = stripslashes($_POST['comment']);

$subject = "Website Enquiry";

$msg = "From : $name rn";
$msg .= "Number : $number rn";
$msg .= "Email : $email rn";
$msg .= "Message : $comment rn";
?>
</head>

The first variable is pretty self explanatory, just input your email address where the example one is:

$to = 'email@example.com';

The second batch of variables read the form data from the html form elements ready to be sent in the email:


$name = stripslashes($_POST['name']);
$number = stripslashes($_POST['number']);
$email = stripslashes($_POST['email']);
$comment = stripslashes($_POST['comment']);

Make sure that these variables are named the same as the id and name attributes of the form elements (which we will come to later).

The final set of variables gather the form data and the output it to your email:


$msg = "From : $name rn";
$msg .= "Number : $number rn";
$msg .= "Email : $email rn";
$msg .= "Message : $comment rn";

The $msg variable is set, then what the form field is called (i.e. Name) and then the data from the form, again make sure these variables are consistant with the ones above and the id and name attributes of the form elements.

Now in the body section of your document, you will need the following code:


<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
$self = $_SERVER['PHP_SELF'];
?>
<form action="<?php echo $self;?>" method="post" id="enquiry-form">
<label for="name">Name:</label>
<input name="name" id="name" type="text" />
<label for="email">Email:</label>
<input name="email" id="email" type="text" />
<label for="number">Telephone:</label>
<input name="number" id="number" type="text" />
<label>Reason for enquiry:</label>
<textarea rows="10" cols="10" name="comment" id="comment"></textarea>
<button type="submit">Submit</button>
</form>

The first part sets the PHP mailer to post the form from that page, make sure this is above your form:


<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
$self = $_SERVER['PHP_SELF'];
?>

The second part is a basic html form, notice that the names and id’s of the form elements are the same as the variables set in the head section.

Now is for the final section of this tutorial and that is what is displayed after the form has been submitted. Place the following code under the form (above).


<?php
} else {
error_reporting(0);

if (mail($to, $subject, $msg, "From: $emailrnReply-To: $emailrnReturn-Path: $emailrn"))

echo nl2br("
<div class="MsgSentt-2">
Thank you $name.<br /> We will get back to you as soon as possible.
</div>
");

else

echo "
<div class="MsgError-2">
<h3>Error!!</h3>
<p>Sorry $name.</b>, your message failed to send. Please try again!</p>
</div>";
}
?>

This last part of the code will check to see if the form has been sent, if it has it will display the message: “Thank you Submitters Name. We will get back to you as soon as possible.” If the form fails to send, the error message below that will be displayed.

So here is our contact.php file:


<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Contact Us</title>
<?php
$to = 'email@example.com';

$name = stripslashes($_POST['name']);
$number = stripslashes($_POST['number']);
$email = stripslashes($_POST['email']);
$comment = stripslashes($_POST['comment']);

$subject = "Website Enquiry";

$msg = "From : $name rn";
$msg .= "Number : $number rn";
$msg .= "Email : $email rn";
$msg .= "Message : $comment rn";
?>
</head>

</body>

<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
$self = $_SERVER['PHP_SELF'];
?>
<form action="<?php echo $self;?>" method="post" id="enquiry-form">
<label for="name">Name:</label>
<input name="name" id="name" type="text" />
<label for="email">Email:</label>
<input name="email" id="email" type="text" />
<label for="number">Telephone:</label>
<input name="number" id="number" type="text" />
<label>Reason for enquiry:</label>
<textarea rows="10" cols="10" name="comment" id="comment"></textarea>
<button type="submit">Submit</button>
</form>

<?php
} else {
error_reporting(0);

if (mail($to, $subject, $msg, "From: $emailrnReply-To: $emailrnReturn-Path: $emailrn"))

echo nl2br("
<div class="MsgSentt-2">
Thank you $name.<br /> We will get back to you as soon as possible.
</div>
");

else

echo "
<div class="MsgError-2">
<h3>Error!!</h3>
<p>Sorry $name.</b>, your message failed to send. Please try again!</p>
</div>";
}
?>

</body>
</html>

And that’s it! Your own simple, fully functional contact form.

» Click Here to see the simple contact form in action
» Click Here to download the php code

Free Graphics, Icons, Tutorials
Free Graphics Free Christmas Vector Icon Graphics Pack 2017Free Fitness Vector Icons Graphics PackFree Camping Vector Graphics PackFree Summer Graphics PackFree File Icon PackFree Fast Food Vector Graphics
Sharing is caring...
Like & Follow
Share the love, share this page! Facebook Twitter Digg Reddit LinkedIn Pinterest Email
Close [X]
The Web Taylor
1000 Lakeside North Harbour Portsmouth, Hampshire PO6 3EN
02392 123358