Affiliates

Adobe GoDaddy.com








WEBMASTER HOW TO:

Page Last Updated: May 15 2009   Bookmark and Share

How to create php contact & feedback form

   This tutorial will show you how to make a contact form that will send client name, message, and email to your email address. Creating a contact and feedback form is a great way to allow your visitors to communicate with you. This lesson will show you how to make a very simple form and mailer.

 

 


What you need to do

You will need to create two php files. One file will be the actual contact form (contact.php) and the other will be the script to send the form (sendcontact.php)

Note

: There are various ways to create a php mail() contact form. However we chose to create an easy to use form for simplicity's sake.

How to use php mail() function

Syntax

mail (to, subject, message, header)

Parameter

 

Description & Explanation

to   This is where the email of the receipient is placed. youremail@youraddress.com
subject   This is where the email subject is placed by the client
message   This is where the contents of the message or comments are placed by the client
header   This is where the name and email address of the client is placed.

Contact.html (.php)

What to do


Copy this form into your html code and it will create the table for your form. You can use either a .html or .php extension.

contact.php

<form name="form1" method="post" action="sendcontact.php">
<table border="0" cellspacing="1" cellpadding="3" align="center">
<tr>
<td>Subject</td>
<td>:</td>
<td><input name="email_subject" type="text" id="email_subject" size="50"></td>
</tr>
<tr>
<td>Message</td>
<td>:</td>
<td><textarea name="client_message" cols="47" rows="7" id="message"></textarea></td>
</tr>
<tr>
<td>Name</td>
<td>:</td>
<td><input name="client_name" type="text" id="client_name" size="50"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="client_email" type="text" id="client_email" size="50"></td>
</tr>
<tr>
<td height="58">&nbsp;</td>
<td>&nbsp;</td>
<td align="right"><br />
<input type="submit" name="Submit" value="SEND"> <input type="reset" name="Submit2" value="CLEAR"></td>
</tr>
</table>
</form>

contact.php browser display


Subject :
Message :
Name :
Email :
   


Sendcontact.php

This is the script that does all the hard work. It will retrieve all the information from your form, organize the information and send it to the email you provide. This script must have a .php extension.

Note: remember to change "youremailaddress@youraddress.com" in the mail() function, to your true email address that will receive this contact message. Dont worry visitors can not see this php file nor the email you specify in the script.
sendcontact.php

<?php

$email = $_REQUEST['client_email'];
// gets client's email address
$subject = $_REQUEST['email_subject'];
// gets client's subject message
$message = $_REQUEST['client_message'] ;
// gets client's message
$name = $_REQUEST['client_name'];
// gets client's name
$header="From: $name <$email>";
// combines client name and email address to create header

if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h2>Invalid email address Please Try Again</h2>";
echo "<a href='javascript:history.back(1);'>Back to Contact Form</a>";
return FALSE;}
/* validates correct email, if email is false the message "Invalid email address Please Try Again" displays with back link.
If true mail will be sent*/

mail("youremailaddress@youraddress.com", $subject, $message, $header);
// mail function email's all info
print "Your email has been sent";

?>



Upload & Test

After you have created both files (contact.html & sendcontact.php) Upload it to your server/hosting account and test your form out. You should receive the email fairly quickly.

References

http://www.w3schools.com/php/php_post.asp
http://www.phpeasystep.com/phptu/8.html
http://in.answers.yahoo.com/question/index?qid=20090408213147AAOc0SP