id: 3
Category: PHP
Rated:
Advantages of PHP form:
- Unlimited use. (no restriction)
- No Ads.
- Fully customizable.
- Other features not available on premade forms.
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 don’t have any that’s ok to.
Second, let’s download the files you will need. If you don’t 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 fields.
First let’s look at the code in its entirety. (Note: You should copy and paste the entire code first, explanations 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>
<?
}
}
}
?>
<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>
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>
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>
$mail->AddAddress("yourname@yahoo.com","Your Site Name"); //TO
#$mail->AddAddress("info@siteaddress.com","Support"); //CC
$mail->Subject = "New Comments from " .$_POST['email'];
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;
<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>
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>
<td>Site URL: </td><td>" .$_POST['site'] ."</td>
</tr>
<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>
<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>
<table width="100%" align="center" cellpadding="3" cellspacing="0" bgcolor="#DDD3DC">
<tr>
<td><div align="center"><strong>Message has been sent</strong></div></td>
That’s all there is to the PHP form. Its pretty simple once you understand the basics and never to hard for a beginner to PHP.
Continue on to learn a simple way to block spammer bots. Part 2


