How To Develop A Simple PHP Contact Form

I assume it’s your responsibility to setup the online contact methods for your clients. One of these secure methods would be to use a convenient contact form that users can submit their messages too. Most of your clients will require an easy to use and fully functioning contact form for their website.

In this tutorial I will show you how to develop a simple contact form using both HTML and PHP. I will also assume that you have a general understanding of HTML and PHP.

The Contact Form In Action

Please take a look at the live demo of the contact form that we will be creating in this tutorial.

Right now it doesn’t look like much, that is because we are focusing on the functionality and not the style. I will leave the CSS completely up to you. I would recommend you taking a look at my css3 expanding contact form for some possible style examples.

You may also have noticed that I have disabled the contact form in the demo. This is because I didn’t want to receive any test messages through it. However, your contact form will of course be a functioning one.

Develop A Simple PHP Contact Form

1. HTML Markup

Lets start with the HTML markup.

If you didn’t know already, when creating a form we always open the form and close the form with the tags below. Always remember to close your form.



Insert the following HTML into the body section of your document.

<h2>Your Title</h2>

<form action="receiving.php" method="POST">

Name:<br><input type="text" name="name" size="40" /><br><br>

Email:<br><input type="text" name="email" size="40" /><br><br>

Phone:<br><input type="text" name="phone" size="40"><br><br>

Message:<br><textarea name="message" rows="3" cols="31" > </textarea><br><br>

<input type="submit" name="submit" value="Submit" />


HTML Breakdown:

The form action and method attribute basically tells the browser how to send the form data to the php document and which document to use to receive the data. Which in our case is a php document which we will be creating in a moment.

The rest of the code is basic html input fields, for example: name, email, phone and message. Followed by a submit button which you can change the name of by editing the value.

2. PHP Receiving Form

Ok, now we will create the second form in PHP.

Start by setting up a new php document and naming it receiving.php then add the following code to your document.


    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $phone = $_POST['phone']; 
    $message = $_POST['message']; 


	$from_add = ""; 

	$to_add = ""; 
	$subject = "Your Subject Name";
	$message = "Name:$name \n Email: $email \n Phone: $phone \n 
Message: $message";
	$headers = "From: $from_add \r\n";
	$headers .= "Reply-To: $from_add \r\n";
	$headers .= "Return-Path: $from_add\r\n";
	$headers .= "X-Mailer: PHP \r\n";
		$msg = "Mail sent";
print "<p> Thank you $name for your message,
    we will be in contact shortly. <a href=\"index.php\">Click here</a>
    to continue </p>" ;


The top part of the code assigns the received data to the variables in the php document and the if(isset function basically confirms that the submit button “is set”. You can apply this to all fields if you wish.

If you do plan on creating more fields for your contact form, make sure you create new labels and variables for those fields.

$from_add This is where you type in the email you want the contact form to say it is has come from. This can be a non existing email address.

$to_add This is the email address you would like the contact form messages to be sent too.

The subject is pretty self explanatory and the $message is the message you want the contact form to send.

The printed message at the bottom is a confirmation message that the user will be directed too once the contact form has been submitted.

That is basically your fully functioning contact form. You can add other functions in like required fields using php, but I will not be explaining that in this tutorial.

I hope you enjoyed this article, please subscribe to our newsletter to stay up to date with our latest web related blog posts.

Author: Jake

Share This Post On