PHP form //
Back //
Contact Us?
Advantages of PHP form:
- Unlimited use. (no restriction)
- No Ads.
- Fully customizable.
- Other features not available on premade forms.
Disadvantages:
None found... :D
Wondering how this site has their forms built? Well, first make sure your host supports PHP and some basic PHP knowledge would help as well, if you dont have any thats ok to.
Second, lets download the files you will need. If you dont know anything about PHP or a beginner its best not to alter any code within the "Script" folder. Please note: I did
not create this form. Credit is given to PHP.com I believe (friend gave to me and i learned how to use it :P)
Download Files.
Once you have downloaded the files next you need to upload the entire folder named scripts.
Next, open a text editor. We need to write the code for a simple form. I will not explain how to set up a basic HTML form, only how to do the PHP in this section even though I will give you the HTML code I use as a basic and how to add feilds.
First lets look at the code in its entirity. (Note: You should copy and paste the entire code first, explinations of each part skip parts which you cannot change or customize).
<?PHP
//send this Form as Email after submit.
if($_POST['send']){
if( (empty($_POST['email']) OR empty($_POST['comments']) OR empty($_POST['name'])) && $_POST['send'] ){
$error = "ERORR 1. - Massage dosen't sent!";
?><br>
<table width="100%" align="center" cellpadding="3" cellspacing="0" bgcolor="#FF3300">
<tr>
<td><div align="center"><strong>Please fill in all the fields that are marked with a *</strong></div></td>
</tr>
</table>
<? }else{
// Email sent...........
require($_SERVER['DOCUMENT_ROOT']."/scripts/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsMail();
#$mail->IsSMTP(); // telling the class to use SMTP
$mail->IsHTML(true);
$mail->From = $_POST['email']; //from E-mail
$mail->FromName = $_POST['name']; //from Name
$mail->AddReplyTo($_POST['email'], $_POST['email']);
$mail->AddAddress("yourname@yahoo.com","AOR"); //TO
#$mail->AddAddress("info@skylike.de","Support"); //CC
$mail->Subject = "New Comments from " .$_POST['email'];
$mail->Body ="
<html>
<head>
<style type=\"text/css\">
<!--
body,td {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333333;
}
-->
</style>
</head>
<body>
<strong>Comments/Questions // AOR</strong><br>
<hr width=400 align=left>
<table width=700 cellpadding=5 cellspacing=0>
<tr>
<td>Name: </td><td>" .$_POST['name'] ."</td>
</tr>
<tr>
<td>Email: </td><td>" .$_POST['email'] ."</td>
</tr>
<tr>
<tr>
<td>-</td><td><strong><hr width=150 align=left></strong></td>
</tr>
<tr>
<td valign=top >Comments:</td><td>" .$_POST['comments'] ."</td>
</tr>
</table>
</body>
</html>
";
$mail->WordWrap = 80;
if(!$mail->Send()){
echo $error = "ERROR 2. - Message wasn't sent!";
echo "<br>error: " . $mail->ErrorInfo;
}else{
// echo "Message has been sent";
?>
<br>
<table width="100%" align="center" cellpadding="3" cellspacing="0" bgcolor="#DDD3DC">
<tr>
<td><div align="center"><strong>Message has been sent</strong></div></td>
</tr>
</table>
<?
}
}
}
?>
This is only the php part of the code. Now we need to break it apart to see what each part does and customize it to fit the purpose of the form. the HTML code used in this form (under the PHP code) is below:
<form action = "#" method="post">
<table>
<tr>
<td>Your name*: </td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>Your E-Mail*</td>
<td>
<input type="text" name="email">
</td>
</tr>
<tr>
<td>Comments*</td>
<td>
<textarea cols="30" rows="5" name="comments">Post your comments here. Thank you.</textarea>
</td>
</tr>
<tr>
<td><input type = "submit" name = "send" value = "Send Comments"></td>
<td><input type = "reset" name = "empty" value = "Empty the form"></td>
</tr>
</table>
</form>
To understand the code, I will break up the PHP code into 3 parts. The "Error Message" if a post wont send, the appearnce of it when you open it into your email and customizeing it, and then the message that says it has been sent.
if($_POST['send']){
if( (empty($_POST['email']) OR empty($_POST['comments']) OR empty($_POST['name'])) && $_POST['send'] ){
$error = "ERORR 1. - Massage dosen't sent!";
?><br>
<table width="100%" align="center" cellpadding="3" cellspacing="0" bgcolor="#FF3300">
<tr>
<td><div align="center"><strong>Please fill in all the fields that are marked with a *</strong></div></td>
</tr>
</table>
The Error Message. There are fields you can make people have entered info, otherwise it will tell them to fill out the areas that aren't filled out. In this error message it says you must fill out the fields called Email, Comments, and Name:
if( (empty($_POST['email']) OR empty($_POST['comments']) OR empty($_POST['name'])) the if statment is saying "if the fields are not filled out, then display the error message upon hitting the Send button. If they are filled out then send it and display 'Message has been sent' message."
You can change what the error message says, just change the text saying "Please fill out all the fields that are marked with a *".
Can I add to fields they have to fill out? Yes you can. Simply within this code you just add
OR empty($_POST['field name']) someplace within the code. The
field name is in the HTml code for the form.
<input type="text" name="email">
Notice where it says "Name = "email" " Email is the name of this input field.
You may notice this error code will show up in a bright green color.
bgcolor="#FF3300" you can change this to change the background color for the message.
$mail->From = $_POST['email']; //from E-mail
$mail->FromName = $_POST['name']; //from Name
$mail->AddReplyTo($_POST['email'], $_POST['email']);
$mail->AddAddress("yourname@yahoo.com","Your Site Name"); //TO
#$mail->AddAddress("info@siteaddress.com","Support"); //CC
$mail->Subject = "New Comments from " .$_POST['email'];
$mail->Body ="
<html>
<head>
<style type=\"text/css\">
<!--
body,td {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333333;
}
-->
</style>
</head>
<body>
<strong>Comments/Questions // AOR</strong><br>
<hr width=400 align=left>
<table width=700 cellpadding=5 cellspacing=0>
<tr>
<td>Name: </td><td>" .$_POST['name'] ."</td>
</tr>
<tr>
<td>Email: </td><td>" .$_POST['email'] ."</td>
</tr>
<tr>
<tr>
<td>-</td><td><strong><hr width=150 align=left></strong></td>
</tr>
<tr>
<td valign=top >Comments:</td><td>" .$_POST['comments'] ."</td>
</tr>
Customizing the form sent to your email can be a very important thing. This helps differentiate any forms you have on your site, like if you have one for contest and another as a contact and another for requesting.
$mail->AddAddress("yourname@yahoo.com","Your Site Name"); //TO
#$mail->AddAddress("info@siteaddress.com","Support"); //CC
$mail->Subject = "New Comments from " .$_POST['email'];
This is an area you
MUST change the info in.
$mail->AddAddress("yourname@yahoo.com","Your Site Name"); //TO this is the address its sending it to. Be sure to add you email address where it says "yourname@yahoo.com" you dont need a yahoo address, it was put there as an example. When you get an email from someone using this form it will have for the subject "New Comments from..." and then their email adress. You can change this if you want easily. For example:
If you want it to be New Entry from....and then their email you'd just change the text. If you'd rather it use the info, such as their name and not email in the subject change the field name from 'email' to 'name' or if you have one named site and you'd like their site address put 'site'.
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333333;
This is basic CSS code for how it will look. its text and the color of the text. This is all changeable.
<strong>Comments/Questions // AOR</strong><br>
<hr width=400 align=left>
<table width=700 cellpadding=5 cellspacing=0>
<tr>
<td>Name: </td><td>" .$_POST['name'] ."</td>
</tr>
<tr>
<td>Email: </td><td>" .$_POST['email'] ."</td>
</tr>
<tr>
<tr>
<td>-</td><td><strong><hr width=150 align=left></strong></td>
</tr>
<tr>
<td valign=top >Comments:</td><td>" .$_POST['comments'] ."</td>
</tr>
Comments/Questions // AOR is the header of the form sent, its title basicly and it will be displayed at the top of the form bolded. You will notice that this area also has the
exact same format as the HTML code the users fill out. This is how it will all be displayed when sent to you.
If you add a field to the HTML code, you'll need to add it to this part of the PHP code as well. But how do you add it?
Lets say I want to add a field called "Site" where people will put their url. First I would add the HTML code to my table:
<td>Your Site*</td>
<td>
<input type="text" name="site">
</td>
Now I must add it to the PHP code, the line of code would look like this:
<td>Site URL: </td><td>" .$_POST['site'] ."</td>
</tr>
I would put that line of code where it is in the HTMl code as well, so the HTML form will look like this:
<form action = "#" method="post">
<table>
<tr>
<td>Your name:* </td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>Your E-Mail:*</td>
<td>
<input type="text" name="email">
</td>
</tr>
<td>Your Site Url:*</td>
<td>
<input type="text" name="site">
</td>
<tr>
<td>Comments:*</td>
<td>
<textarea cols="30" rows="5" name="comments">Post your comments here. Thank you.</textarea>
</td>
</tr>
<tr>
<td><input type = "submit" name = "send" value = "Send Comments"></td>
<td><input type = "reset" name = "empty" value = "Empty the form"></td>
</tr>
</table>
</form>
The PHP part would look like this:
<strong>Comments/Questions // AOR</strong><br>
<hr width=400 align=left>
<table width=700 cellpadding=5 cellspacing=0>
<tr>
<td>Name: </td><td>" .$_POST['name'] ."</td>
</tr>
<tr>
<td>Email: </td><td>" .$_POST['email'] ."</td>
</tr>
<tr>
<td>Site URL: </td><td>" .$_POST['site'] ."</td>
</tr>
<tr>
<td>-</td><td><strong><hr width=150 align=left></strong></td>
</tr>
<tr>
<td valign=top >Comments:</td><td>" .$_POST['comments'] ."</td>
</tr>
The only part left to look at is the 3rd part.
<table width="100%" align="center" cellpadding="3" cellspacing="0" bgcolor="#DDD3DC">
<tr>
<td><div align="center"><strong>Message has been sent</strong></div></td>
This is where if the user filled out all the fields you wanted the user to fill out, will send the form and tell you that the form was sent. To change the background color of the message you just need to edit
bgcolor="#DDD3DC" You can also change the message if you want.
Thats all there is to the PHP form. Its pretty simple once you understand the basics and never to hard for a beginner to PHP.
Back //
Contact Us?