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> </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.
{ 11 comments… read them below or Shout @ me! }
New blog post: How to create a Simple Php Contact form http://bit.ly/PUsHa
This comment was originally posted on Twitter
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
Thanks. I have tested it. But I can never recieve an e-mail?
@Mel
Did you changed the contents of process.php to incorporate your email id, Put the code you are using.
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.
You need to change the email specified in PHP file and that will work.
Hope it helps
I rarely plan on that happening. ,
Dude. I followed your instruction by the word and it didn’t work at all!
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
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
Thanks Man Thanks Again ….