Tutorial: Creating a form in PHP and XHTML.
This is the first of many PHP email PHP scripts. As you read on you will find out what else I have in store for you.
About 6 months ago I had to create my own PHP form to use with a website I was working with. After completing it, I was shocked at how easy it is. In this tutorial I will show you the basic’s of the PHP script and what it all means. After that, I will show you how to code it in your website.
Let’s take a look at a custom PHP script. After that, I will break it down for you so you get an idea what what you are looking at.
PHP Code
<?php
$EmailTo = "matt@mattginsberg.net";
$Subject = "Subject Here";
$fname = Trim(stripslashes($_POST['fname']));
$lname = Trim(stripslashes($_POST['lname']));
$from = Trim(stripslashes($_POST['from']));
//validation
$validationOK=true;
if (Trim($fname)=="") $validationOK=false; // user first name
if (Trim($lname)=="") $validationOK=false; // user last name
if (Trim($from)=="") $validationOK=false; // user email
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "First Name: ";
$Body .= $fname;
$Body .= "\n";
$Body .= "Last Name: ";
$Body .= $lname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $from;
$Body .= "\n";
$Body .= Comments: ";
$Body .= $comments;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From: <$from>");// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}?>
Now, lets break it down so you can understand it better. We will start at the top and work our way down
PHP Code
<?php
$EmailTo = "matt@mattginsberg.net";
$Subject = "Subject Here";
$fname = Trim(stripslashes($_POST['fname']));
$lname = Trim(stripslashes($_POST['lname']));
$from = Trim(stripslashes($_POST['from']));
This PHP code tells the form where it is going. First thing is first, the command <?php always starts your PHP script. Now, if you notice the EmailTo, that is your email address. The Subject is listed there as well, I would assume you understand what that means. You would want to type out the actual subject. Example, if you are putting this php/html form in your quote section of the website, then you would type in – Quote. However, lets take a look at $fname, $lname, and $from. The $fname (firstname) is the name of the text feild in your HTML form. The name would be fname, not $fname. Same thing for lname and from. $from is the end users (or person submitting the form) email. Lets move on.
PHP Code
$validationOK=true;
if (Trim($fname)=="") $validationOK=false; // user first name
if (Trim($lname)=="") $validationOK=false; // user last name
if (Trim($from)=="") $validationOK=false; // user email
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
This PHP reviews the items that are required to be filled out by the end users. If you do not reguire this, then do not put it in. You will notice a if () command. If the end user doesn’t fill out a text feild, check box, bullet, they will be directed to an error page that you specify. You must create one!! Lets move on.
PHP Code
// prepare email body text
$Body = "";
$Body .= "First Name: ";
$Body .= $fname;
$Body .= "\n";
$Body .= "Last Name: ";
$Body .= $lname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $from;
$Body .= "\n";
$Body .= Comments: ";
$Body .= $comments;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From: <$from>");
This PHP code is the body of the email you receive when the end user submits their form. The command $body will NOT be listed in your HTML form. The $success command will look at the validation and then will send the email to you with the subject you provided along with the body (first name, last name, email and comments). Lets move on.
PHP Code
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
This PHP code is mainly for the $success. If the validation is correct, your form will take you to the ok.htm file (that you create and put in the same directory at this php file). If the $success failed, it moves to the else {} command, which tells the php form to send it to the error.htm page that you also created. The error.htm page within the $success can be different than the error.htm page within the validation section of this code. Specify one as error1.htm and error2.htm. Also, the command ?> is used to close the PHP script.
Lets move on to the HTML Code.
HTML Code
<p><font color="#EE0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">*</font><font size="2" face="Verdana, Arial, Helvetica, sans-serif">required</font></p>
<p><font color="#EE0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">*</font><font face="Verdana, Arial, Helvetica, sans-serif">First Name:</font>
<input name="fname" type="text" id="fname" size="20" style="margin-left:50px">
<br>
<br>
<font color="#EE0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">*</font><font size="2" face="Verdana, Arial, Helvetica, sans-serif"></font><font face="Verdana, Arial, Helvetica, sans-serif">Last Name: </font>
<input name="lname" type="text" id="lname" size="20" style="margin-left:49px">
<br>
<br>
<font color="#EE0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">*</font><font size="2" face="Verdana, Arial, Helvetica, sans-serif"></font><font face="Verdana, Arial, Helvetica, sans-serif">Email: </font>
<input name="from" type="text" id="from" size="20" style="margin-left:92px">
</p>
<p><font face="Verdana, Arial, Helvetica, sans-serif">Comments:</font>
<textarea name="comments" id="comments" cols="25" rows="5" style="margin-left:62px"></textarea>
</p>
<p> </p>
<p>
<input type="submit" name="button" id="button" value="Submit">
</p>
Hint: Make you you enter the proper form tags before and after the above XHTML code. If you are unaware of what they are, please let me know.
This basic HTML will get the code job for you. Just copy and paste the PHP and HTML into your web site editing software and have fun.
Stay tuned, I am going to create more PHP tutorials. These will be considered addons. They will include, a ‘cc me’ checkbox, email validation, and a captcha. The cc me will be done with PHP, the other will be done with JavaScript.
The cc me is when the end user wants a copy of the form they submitted to you. The email validation will not allow people to type in asdfasf@adsfasdf.com. It will make sure it is a proper email. The captcha will put a simple picture with numbers that the end user needs to fill in. In doing so, they will submit the form. Those to come sooner than later. Why dont you subscribe to my blog?
If you have any questions at all, please contact me.










I'mma be what I set out to be, without a doubt undoubtedly - And all those who look down on me I'm tearing down your balcony ~ Em.
Please leave your comment!