PHP3 Introduction - HTML forms and variables (
Page 3 of 4 )
Another powerful feature of PHP3.0 is it's
capability of modifying variables passed from HTML forms. With these variables,
one can accomplish many a feat, including such tasks as: sending web-based
email, outputting information to the screen, and passing data to and from a
database. Let's construct a small automated email program, demonstrating many of
these capabilities:
Let's assume we have the following HTML
form:
<HTML>
<HEAD>
<TITLE>Request for more information</TITLE>
<BODY>
<CENTER>Would you like more information about our company?
<P>
<TABLE WIDTH = 400><TR><TD align = right>
<FORM ACTION="email.php3" METHOD="POST">
Your name:<BR>
<INPUT TYPE="text" NAME="name" SIZE="20" MAXLENGTH="30">
<P>
Your email address:<BR>
<INPUT TYPE="text" NAME="email" SIZE="20" MAXLENGTH="30">
<P>
I prefer:
<SELECT NAME="preference">
<OPTION value = Apples>Apples
<OPTION value = Oranges>Oranges
</SELECT>
<P>
<INPUT TYPE="submit" VALUE="Send it!">
</FORM>
</TD></TR></TABLE></CENTER>
</BODY>
</HTML>
Save the above HTML as
moreinfo.html.
Notice that the
ACTION points to the file
email.php3. This file will contain the PHP3.0
script that will carry out several commands.
The
email.php3
file:
<?
/* this script will handle the variables passed from the moreinfo.html file */
PRINT "<CENTER>";
PRINT "Hello, $name.";
PRINT "<BR><BR>";
PRINT "Thank you for your interest.<BR><BR>";
PRINT "We will send information to $email, and have noted that you like $preference.";
PRINT "</CENTER>";
?>
Don't forget to save the above file as email.php3.
When the
user types in their name and email within the HTML form, and presses the "Send
it!" button, the form will call the email.php3 file, in turn returning the
following output (assuming the person's name is Bill, has the email address
bgates@devshed.com, and likes Apples):
Hello, Bill.
Thank you for your interest.
We will send
information to bgates@devshed.com, and have noted that you like Apples.
However, our project is not yet complete, as we would not know
who had inserted any information, since we are not keeping any records of what
happened. Thus we would not be able to send Bill an email.
One way to do
so, in turn lessening the work load of sending standard email messages manually,
is by implementing PHP3.0's MAIL() command.
Syntax: void mail(string to,
string subject, string message, string add_headers);
- to - the whom the email is directed.
- subject - phrase to be inserted in subject line of email message.
- message - the actual message.
- add_headers - use this to insert a string at the end of the header.
(optional)
Thus, if we inserted the following commands after the
last PRINT statement in the preceding script, we could automatically send email
both to the person requesting information, and to the site administrator,
letting us know who requested the information:
<?
mail("$email", "Your request for information", "$namen
Thank you for your interest!n
We sell fresh corn daily over the Internet!
Place your order at http://www.buycorn.com,
and receive a free package of $preference!");
mail("administration@buycorn.com",
"Visitor request for info.",
"$name requested for information.n
The email address is $email. n
The visitor prefers $preference.");
?>
Important Note: The MAIL() function only works when SENDMAIL is
installed on the server. In most cases, it is. However, be sure to check with
your ISP before attempting to use this function.
But what happens when so
many users are entering information, that you can't keep track of all of the
emails? Or, you require a system to track how many prefer Apples to Oranges? The
implementation of a database aids greatly in the administration of these types
of data. One of the fastest database server on the market,
MySQL, is a great choice for reasons of speed and
ease of use, as well as it's flexibility and compatibility with
PHP3.0.
The next section will deal with the incorporation of the MySQL
database with PHP3.0.