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.