How to create a Simple Php Contact form

by dhiraj on July 28, 2009

Here I am going to discuss a most common and I think one of most necessary component which every body require for their blog or site. So, friends here is the code of a simple contact form. how to create it and how to handle it. Generally it is required that if somebody is filling the form then the information should be sent as mail and/or get stored in database. In this post, we will only discuss the mail part and you may add the another functionality.

Let’s start..!! What we need for a contact for is a form first of all. basically we will need two files :

  • contact.php : this will contain the html code for the contact form.
  • process.php : this file will have necessary php coding for handling the above contact form.

//

The contact.php will look like :

<form name="contact" method="POST" action="process.php">
<table width="456" border="0" cellpadding="0" cellspacing="0">
   <tr>
      <td height="28" valign="top">Your name:</td>
      <td><input name="name" type="text"></td>
   </tr>
   <tr>
      <td height="28" valign="top">E-mail address:</td>
      <td><input name="email" type="text"></td>
   </tr>
   <tr>
      <td height="28" valign="top">Contact No:</td>
      <td><input name="contact" type="text"></td>
   </tr>
   <tr>
      <td height="28" valign="top">Message:</td>
      <td><input name="message" type="text" /></td>
   </tr>
   <tr>
      <td>&nbsp;</td>
      <td valign="top" align="center"><input type="submit" value="Submit" name="submit"></td>
   </tr>
</table>
</form>

now we will talk about process.php. This file should process the contact form and mail the all information filled in the contact form.

the process.php file will look like :

<?php
if(isset($_POST['submit'])) {
   $to = 'xyz@gmail.com' ;     //put your email address on which you want to receive the information
   $subject = 'hello';   //set the subject of email.
   $headers  = 'MIME-Version: 1.0' . "\r\n";
   $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
   $message = "<table><tr><td>Your Name</td><td>".$_POST['name']."</td></tr>
               <tr><td>E-Mail</td><td>".$_POST['email']."</td></tr>
               <tr><td>Contact No</td><td>".$_POST['contact']."</td></tr>
               <tr><td>Message</td><td>".$_POST['message']."</td>
               </tr></table>" ;
   mail($to, $subject, $message, $headers);
   header('Location: contact.php');
}
?>

Put both files in the same directory and yes …!! this is just a basic contact form. You can put lots and lots of more functionality in it. So, go ahead, may this post help you in any manner.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

{ 22 comments… read them below or Shout @ me! }

phpcollection July 28, 2009 at 7:12 pm

New blog post: How to create a Simple Php Contact form http://bit.ly/PUsHa

This comment was originally posted on Twitter

Reply

abhinavsahai July 29, 2009 at 4:38 am

FB RT: Dhiraj Pandey RT @phpcollection How to create a Simple Php Contact form | PHP tutorials and Scripts Collection http://bit.ly/CEH2r

This comment was originally posted on Twitter

Reply

Mel August 30, 2009 at 2:14 pm

Thanks. I have tested it. But I can never recieve an e-mail?

Reply

Php Ninza August 30, 2009 at 4:21 pm

@Mel

Did you changed the contents of process.php to incorporate your email id, Put the code you are using.

Reply

Jameel Ahmad Siddiqui October 12, 2009 at 6:40 am

Sorry, but form not working. could it possiable that you make it easy? i mean just HTML cod to past in document and php cod for anyname.php.

Reply

Php Ninza October 12, 2009 at 5:16 pm

You need to change the email specified in PHP file and that will work.

Hope it helps

Reply

John10 October 22, 2009 at 8:01 am

I rarely plan on that happening. ,

Reply

aj23 December 3, 2009 at 4:43 pm

Dude. I followed your instruction by the word and it didn’t work at all!

Reply

Php Ninza December 22, 2009 at 8:41 am

I am getting several mails and it seems you have not changed email id in the php code. Please do that so you can recieve the emails not me ;)

Reply

Luca M June 28, 2010 at 11:11 pm

Thanks for the helpful tutorial. I just have one issue with the code. Once the message is submitted, I get an error warning on the screen.
Would you please be able to tell me what code I need to say “Thank you for your message” instead? or redirect them to a different page.
It would be extremely appreciate and helpful since that is the last issue I need to solve.
Thanks, Luca

Reply

Faisal July 10, 2010 at 10:13 am

Thanks Man Thanks Again ….

Reply

tn9ae July 27, 2010 at 10:46 pm

Tried it , used my email address , but it never sends to my inbox !

Reply

Jessica Calzita August 21, 2010 at 6:49 am

i tried it.. i encountered no errors but i can never receive an email..all i want to happen is when you click the submit button.. the message he enter will directly send to my yahoo email(jheyven_05@yahoo.com)..can you help me to do this way??please.

Reply

Fred Loomis February 22, 2011 at 4:07 pm

This did not work for me. I believe the server running the apache2, php needs to have a mail server configured. if this is the case, how does one do this?

Reply

she February 23, 2011 at 2:31 am

I tried to run it in dreamweaver but it says open or save php file. whats wrong it that, help please

Reply

Connor Barrett May 2, 2011 at 1:32 am

Hay, could you please email me the HTML code for the comment form a the bottom of the page.
Thanks

Reply

Avinash Arora June 1, 2011 at 5:07 pm

Thanks! This is so simple and brilliant. Very easy to implement and edit :) I think I got it from seeing this website to live on my site in approximately 40-50 seconds.

Reply

Learn PHP July 10, 2011 at 2:06 am

Thanks Avinash. I am glad that you find this code useful.

Reply

Amit June 3, 2011 at 9:20 am

What is the code for multiple page contact form? Say I have 10 fields and I put the 5 fields on first page with a button to next page and rest 5 fields on second page with a submit button. On pressing the submit button the email must be sent to the specified address with all 10 fields.

Reply

Learn PHP July 10, 2011 at 1:59 am

You will need to change the code to take POST values to the next page and create sessions to make sure that data stays alive while next page refresh. Looking at POST and SESSION variables documentation shall help.

Reply

mathan October 9, 2011 at 1:04 pm

thanks bro. it’s helpful

Reply

Jack Rimell October 16, 2011 at 4:14 pm

Thanks, this worked great and so easy. Allot easier than using SMTP and CLASS.PHPMAILER.PHP thanks again

Reply

Leave a Comment

Previous post:

Next post: