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 = 'dhiraj.cs@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

{ 11 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

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

Mel August 30, 2009 at 2:14 pm

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

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.

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.

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

John10 October 22, 2009 at 8:01 am

I rarely plan on that happening. ,

aj23 December 3, 2009 at 4:43 pm

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

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 ;)

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

Faisal July 10, 2010 at 10:13 am

Thanks Man Thanks Again ….

Leave a Comment

Previous post:

Next post: