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.

<form>

</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" />
<br><br>

</form>

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.

&lt;?php 

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

if(isset($_POST['submit']))

{
	$from_add = "contactform@yourwebsite.com"; 

	$to_add = "yourname@yourwebsite.com"; 
	
	$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";
	
	
	if(mail($to_add,$subject,$message,$headers)) 
	{
		$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>" ;
	


?&gt;

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
  • http://alplist.com/story.php?id=55095 learn more

    I’ve been surfing online more than 4 hours today, yet I never found any interesting article like yours. It’s pretty worth
    enough for me. In my opinion, if all site owners and bloggers made good content
    as you did, the net will be a lot more useful than ever before.

    • http://www.develop-a-website.com Jake

      Hey, thanks for the awesome comment! It’s what keeps me going. I hope that the form was exactly what you where looking for. I wrote it because I could never find a contact form myself, I’m really glad its become of use for someone other than myself. If you are looking for information on anything else, just let me know. Jake.

  • Al

    Nice form Jake, thanks for sharing, was looking for something simple for a while now and this seems to be the simplest. I don’t know much about PHP, anyway you could help me redirect the thank you message back to the form instead of a new ‘blank’ page?

    • http://www.develop-a-website.com/ Jake

      Hey Al, It is actually supposed to direct you to a thank you message which is at the bottom of the php document. If it isn’t working, make sure that your ” form action=”receiving.php ” from your HTML document (without the quotes) is being directed to the correct php document. The php document needs to be named receiving.php or the name you have labeled it. Let me know if that works for you.

      • Al

        Thanks for the reply. I did have it working, but what I wanted to happen was when someone fills out the form it gets redirected back to the form with a thank you message. I’ve seen it on a few sites, just never knew how it was done. Thanks anyway Jake. It’s for my portfolio site, will post a link when it’s done 😉

        • http://www.develop-a-website.com/ Jake

          Oh ok you mean a thank you message above or underneath the form? Yeah sure post a link of your site when its finished.

          • Al

            Yea exactly what I mean. Should have the site ready very soon. So would it be hard to redirect the ‘Thank You’ page back to the form to just show a line probably above the form confirming whether it has been sent or not? Not exactly sure on this kind of stuff.

          • http://www.develop-a-website.com/ Jake

            I would have to re write the form. It isn’t hard but I like this form to re direct to a blank page. But I do know of a
            video tutorial that writes the php form in the way you want it to work. It also teaches you some other php things like error messages and checking if certain elements have been set? Would you like me to send you the link? It’s a 4 part tutorial but you could have it ready within 20minutes or so.

          • Al

            Yea that would be great, thanks!

          • http://www.develop-a-website.com/ Jake

            Sent the link to your email, good luck!

  • Tom

    Not working for me. Message not sent. Always redirect to new blank page without sending message. Need help here. So Frustrated.

    • http://www.develop-a-website.com/ Jake

      Hi Tom, sorry to hear that. Paste me your code, I’ll take a look for you.

      • Tom

        Here’s my html http://pastebin.com/HGUe3ZV4
        and this is my php file http://pastebin.com/e8r6jvZE

        • http://www.develop-a-website.com/ Jake

          Hi Tom, Sorry for the delay I’ve been working all week. I see what the issue is, its calling the double quotes at the end of the php file. Where it says thanks for contacting us, you need to escape this. I’m going to add it to my code now.

          I’ve added the two backslashes before the quote and after the .php – I’ll modify my code. Let me know how you get on.

  • http://www.develop-a-website.com Jake

    Your welcome :)