4 Basic Steps to Create an Email Form with PHP Script
Introduction
According to Susan Anderson-Freed, author of the book "Weaving a Website",
after HTML, the three other computer languages that can be used for web
site development, are (in the order of the easiest to the more difficult):
JavaScript, Perl, and Java. Another computer language that is also
difficult, but can be learned with determination, is PHP. Though PHP
as a computer language needs to be learned with accuracy and precision,
it is possible to know the basics of the language in order to create an
email form for a web site. After consulting many books and searching
online for tutorials to create an email form with PHP, I came up with
something which you can also use as a reference to create your own
PHP script.
I will divide the process in 4 basic steps.
Step 1: Create an email form embedded in an .html page
The purpose of an email form is for the visitor of the web site to input
his information, such as full name and email address, so that it could be
sent to the web site administrator's email address. The email form will
depend on what service the web site is offering its visitors. One form of
service that can be rendered is a newsletter. For this purpose, the
following code can be used to create the email form for the visitor who
wants to subscribe to the web site's newsletter:
<FORM method="POST" action="myphpscript.php">
<p>Your Name: <INPUT
type="text" name="sender_name" size="30">
</p>
<p>Your E-Mail Address:
<INPUT type="text" name="sender_email"
size="30"></p>
<p>Subscribe to newsletter by e-mail?
<INPUT type="radio" name="subscribe"
value="Yes"></p>
<INPUT type="submit" value="Send">
</FORM>
(Note: This form is to be embedded in an .html page of your preferred web design. It
would be best to save the .html page in the same directory where you intend to place
your php script.)
Step 2: Create the PHP script in stages
The next step is to create a myphpscript.php that will get all the inputs the
visitor placed in the form, and post it to your email address.
The opening and closing tags for a PHP script are:
<?php and ?>
We need to identify well the variables for the PHP script.
For this, we will create three lines in the script:
$sender_name = $_POST['sender_name'];
$sender_email = $_POST['sender_email'];
$subscribe = $_POST['subscribe'];
(Note: Don't forget the ";" symbol to close each line of php.)
After identifying well our variables for the PHP script, we will then create
a message string, that will format the inputs obtained from the form and
post it to our email address.
So, the next three lines in the PHP script would be:
$msg .= "Sender's Name:\t$sender_name\n";
$msg .= "Sender's E-mail:\t$sender_email\n";
$msg .= "Subscribe:\t$subscribe\n\n";
Before this message string, we also can add the following code to
create a basic format for the email message the script will process
and send to our email inbox.
$msg = "From: My Web Site\n";
$msg .= "Reply-To:\t$sender_email\n";
(Note: Always remember to terminate each line with a ";" Forgetting
to do so will produce an error in the script.)
Step 3: Complete the PHP script and save as myphpscript.php file
The whole PHP script in its complete form is given as follows:
<?php
$sender_name = $_POST['sender_name']
$sender_email = $_POST['sender_email']
$subscribe = $_POST['subscribe']
$msg = "From: My Web Site\n";
$msg .= "Reply-To:\t$sender_email\n";
$msg .= "Sender's Name:\t$sender_name\n";
$msg .= "Sender's E-mail:\t$sender_email\n";
$msg .= "Subscribe:\t$subscribe\n\n";
mail("youremail@yourdomain.net", "Website Mail", $msg);
echo "<H2 align="center">Thank you,
$sender_name</H2>";
echo "<P align="center">Kindly click the back
button to return.</P>";
?>
The first of the last three lines of the script would send the inputs of
the form to the email address we specify in the mail( ) function. The subject
of that email would be "Website Mail". The last two lines of the PHP script
will present a "Thank you" message to the sender and guide him back to the page
which contains the email form. In case the sender forgets to input an
information, he can always enter again his inputs and resend it to the email
address we designate for this purpose - such as youremail@yourdomain.net.
Step 4: Saving the email form and the PHP script
For the email form and the PHP script to work together, we need to upload both files
in the same directory of our web hosting account's file manager. We save
the email form as a .html file, and the script as a .php file. We need to test
and re-test the form and the script until all the desired results are obtained.
The obtained results should be: the sending of all the visitor inputs in the desired
format to our email address, and the "Thank You" message with the visitor's name
displayed in the browser. There is a more advanced way in composing the PHP script
so that in case any of the inputs are not given by the subscriber, he will be reminded
to input it. However, for a small Catholic web site, the basics are enough. He can
always resubmit his personal information in case he does not receive the subscription.
Conclusion
Learning to create an email form with PHP script is very challenging work. First,
we need to consider the server of our web hosting account. Then, we also need
to learn what PHP version would work with that server. It is really a trial and
error process that we need to be patient with, before we finally get that right
combination of variables, tags, formats, message string, and other important PHP
script details, to make everything work. If we do not give up and go through this
learning process patiently, the joy of seeing our work bear produce is the reward
for all our efforts. "Patient endurance attains to all things".
Related resources
- PHP Essentials by Julie C. Meloni
- PHP & MySQL by Vikram Vaswani
Get other ideas and updates on basic and
small web site development for Catholics
If you intend to follow the methods presented in this web site - both
the basic and the advanced, you can choose any one of the following:
Published Articles Directory
Page 1 |
2 |
3 |
4 |
5
Dennis Emmanuel Cabrera
Web Developer Methods for Catholics
www.c-web-developer.net
My Main Catholic Blog
www.c-internet-mission.net
|