Basic PHP Contact Form

posted: category: web-design

Basic PHP Contact Form

Every website should have some way to easily submit an inquiry. In this lesson, I'm going to explain the steps required to create a basic contact form. This will be accomplished, using both PHP and HTML. You are going to need to create two files, an HTML file where you will display the contact form and a PHP file to submit it. First, let's create the HTML contact form.

In this example, I'm going to use a blank page with just the contact form. Here is the form itself, then I will explain what it all means.

<!DOCTYPE html>
<title>Basic Contact Form</title>

<form enctype='multipart/form-data' action='send_email.php' method='post'>
<td>Name:</td><td><input type="text" name='name'></td></tr>
<tr><td>Email:</td><td><input type="text" name='email'></td></tr>
<tr><td>Comment or Question:</td><td><textarea name='comments' rows=5 cols=15> </textarea></td></tr>
<tr><td><input type="reset" value='Reset Form'></td><td><inpu

t type="submit" value='Submit Form'></td>


That is the html part of creating a contact form. As you can see, I used the html tags <form> to create the form. The other html tags used are <input> and <textarea>. The <form> tag creates the form. The <input> tags are used to insert input boxes in the form. The <textarea> tags are used to create a text box for comments.

<form enctype='multipart/form-data' action='send_email.php' method='post'> Method is set to 'post', the other option is 'get', post allows us to send this information to the php file without being sent as a line of html. This is more secure. Action='send_email.php' is telling the form where the php file is located that is going to do something with the information.

<input name="email" type="text" /> This creates the input where users will enter information. The name='' allows the php file to reference this input. The type='' tells us what type of input this is. There are quite a few different options for the input type, here is a brief overview:

button, checkbox, color, date, datetime, email, file, hidden, image, month, number, password, radio, range, reset, search, submit, tel, text, time, url, week

For the most part, the different types of "type" are fairly self-explanitory. If you want a detailed explaination of these types, check out this page from w3schools.

I enclosed the form in a table, for an easier way to later style the contact form.

Comment or Question:


Now for the PHP file. This is the minimum php coding required. For further security to prevent spam, there are verifications methods that you can add. We'll discuss those later. Here is the php code:


// This gives the location of the post information, no need to edit that


// This tells the php file what information it is grabbing from the form

$Name = $_POST['name'];

$Email = $_POST['email']; // This is the name= from the form

$Message = $_POST['comments'];

$sendthis="Name: ".$Name."// This compiles all info into a single string
Email: ".$Email."
Comment: ".$Message."
$sendthis = stripslashes($sendthis);

// This is the actual code that sends the email

mail("","Basic Contact Form",$sendthis,"From: Cloudy Mouse Blog");

 // This is where the user goes after successfully submitting a form

header("Refresh: 0;url=");

That is the basics of the php file. Edit your email to the email where you want to receive the contact form. Where it says "Basic Contact Form" is the email subject line, change this to the subject line you would like to include. The same applies to "From: Cloudy Mouse Blog", this who the email will say it is from.

Save the php code into a php file called send_email.php (Or whatever you refer to in the html for action=""), the permissions will need to be set to 6-4-4, for php file. That is all there is to creating a basic contact form. 

The table that houses the form can be styled however you want, that is why I prefer to use tables for forms.

comments powered by Disqus

About the Author

Jeff K.

Jeff K.

Jeff is a web designer and graphic artist. In addition to web design, Jeff enjoys other forms of art, including pencil sketching and illustration.

If you are looking for affordable web design services for your business, Cloudy Mouse provides timely personalized service. Our focus is creating great looking responsive website designs for small businesses without breaking their bank.

Take a look at our Web Design Services and discover how we can get your business into the cloud.