How to create a Simple Php Contact form

Here I am going to discuss a most common and necessary component which everyone requires for their blog or website, which is a contact-us form. Below you will find the code of a simple contact form. Also, I will discuss how to create the form and handle its submission. Generally We require that when someone is filling the form, its information will be either stored in a database or will be mailed to a given email address. In this post, we will only discuss the email part. You are welcome to modify and store the information in database with very small tweaks.

Let’s start..!! We need files, one to display the form and other one to capture its submission:

  • contact.php : this will contain the html code for the contact form.
  • process.php : this file will have necessary php cods 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 lets have a look on process.php. This file will capture the information of contact form and will email to the given address.

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' . "rn";
   $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
   $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 various enhancements to it. Feel free to comment in case of any issue or suggestion.

43 thoughts on “How to create a Simple Php Contact form”

  1. 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

  2. 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.

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

  4. 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.

  5. 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.

  6. 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.

  7. @John It’s coming via the server email address. The SMTP relay installed on the server. How you’d like it to be changed ?

  8. Hello,

    I get you get loads of questions about this, but I would really appreciate the help. 🙂

    I put process.php on my server and the email does get sent, it sits nicely in my inbox now, I just get an error with the header ‘Location: contact.php’. How do I solve this? Because people will just email me thousands of times, thinking the form is faulty, while I know it’s not.

    Thanks tons, I love this form already!

  9. Looks like you have left a whitespace before "< ?php" tag. This can happen while copying the code from the post.

    I hope that solves your problem.

  10. Pleaseeee. help me. why the message did not go into my inbox. i’ve tried so many times. and i’ve changed the email already. Please help to figure out this problem. pleaseee.

  11. Hi there!
    Great script!

    1 thing, I have the form working just fine but when I receive the email it comes from this: coas4191

    is there anyway i can change that to the email / name entered in the form?

  12. That is the server name. Yes, you can change the from name. In the $to section, just put in $to = ‘User Name ‘;

    Hope that helps.

Leave a Reply