The best way to add
interactivity to your site
with PHP is by adding a
comment form. We can do this
the same way we did with the
Hello $name script, by using
variables. The only
difference this time is that
we'll be using the mail()
function, which is what
sends the user's input to
your email address.
First, we create the HTML
form:
<form
action="formmail.php"
method="post">
Name: <input
type="text"
name="name"><br>
Email:
<input
type="text"
name="email"><br>
Comments: <textarea
name="comments"></textarea><br>
<input
type="submit">
</form> |
|
Then we create the "formmail.php"
file:
<?php
// your
email
address
$youremail =
"kali@xentrik.net";
// field
validation
if
($email==""
||
$comments==""
||
$name=="")
{
print ("All
fields are
required!
Please go
back and try
again.");
}
else {
// send
email
$headers =
"From:
\"$name\"
<$email>\n";
$subject =
"Feedback
Form";
$message =
"$comments";
mail ("$youremail",
"$subject",
$message,
$headers);
print
("Thank you
$name, your
email has
been
sent.");
}
?> |
|
Now, what does it mean?
$youremail = "kali@xentrik.net";
You should be familiar with
this part. Here we're
setting the variable "$youremail"
to your email address.
if
($email=="" || $comments==""
|| $name=="")
Here we're saying "if the
$email, $comments or $name
fields are empty, perform
the action defined between
the {} tags." In PHP code,
|| is an operator which
means "or." We use two
equals signs (==) when we're
comparing, rather than
assigning.
else {
This says "if the above
statement is false (none of
the fields are empty), then
do the following:"
$headers = "From: \"$name\"
<$email>\n";
$subject = "Feedback Form";
$message = "$comments";
Here we're defining the
variables needed to send the
email. $headers displays who
the email is sent from in
the "From" field of the
email. $subject sets the
subject of the email, you
can set it as anything you
want, as you're the only one
who will see it. $message is
the body of the email.
mail
("$youremail", "$subject",
$message, $headers);
This part sends the
variables we defined above
as an email.
That's it, a simple formmail
script, ready to go.
However, it is a good idea
to include an email address
verification function, to
make sure the user puts in a
valid email address. To do
that, we add this bit of
code after the else { line:
// email
validation
if(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$',
$email)) {
print ("Your
email
address does
not appear
to be valid.
Please go
back and try
again.");
exit;
} |
|
This says "If $email is not
in a name@domain.com format,
perform the print() function
stated."
So, just so you're not
confused, here is what the
code all together looks
like:
<?php
// your
email
address
$youremail =
"kali@xentrik.net";
// field
validation
if
($email==""
||
$comments==""
||
$name=="")
{
print ("All
fields are
required!
Please go
back and try
again.");
}
else {
// email
validation
if(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$',
$email)) {
print ("Your
email
address does
not appear
to be valid.
Please go
back and try
again.");
exit;
}
// send
email
$headers =
"From:
\"$name\"
<$email>\n";
$subject =
"Feedback
Form";
$message =
"$comments";
mail ("$youremail",
"$subject",
$message,
$headers);
print
("Thank you
$name, your
email has
been
sent.");
}
?> |
|
|