|
|
|
|
|
|
| |
In Chapter 2, "Basic PHP Techniques" of PHP Essentials, I showed you how to send an feedback form using two files: an HTML file for the form and a PHP script for the form action. In this tutorial, you'll learn how to use the $PHP_SELF variable in a form action, and integrate your own error messages upon form submission. |
|
Start with a simple HTML feedback form, with fields for name, e-mail address and message, only change the file extension from *.html to *.php (or whatever you use on your system) and stick the code for the form within a variable called $form_block. For example, open your text editor and create a file called "myform.php", and in it place the following code: |
|
<HTML>
<HEAD>
<TITLE>Feedback</TITLE>
</HEAD>
<BODY>
<?
$form_block = "
<FORM method=\"POST\" action=\"$PHP_SELF\">
<P>Your Name:<br>
<INPUT type=\"text\" name=\"sender_name\" size=30></p>
<P>Your E-Mail Address:<br>
<INPUT type=\"text\" name=\"sender_email\" size=30></p>
<P>Your Message:<br>
<textarea name=\"message\" cols=30 rows=5></textarea>
</p>
<P><INPUT type=\"submit\" value=\"Send This Form\"></p>
</FORM>
";
?>
</BODY>
</HTML>
|
|
|
Why on earth did I stick all that HTML inside a variable, you ask? Later, we'll need to display certain code based on certain actions, and it makes it a lot simpler to just print specific chunks of text, until you get a handle on what's going on. |
|
|
The plan is to use the global variable $PHP_SELF, which has a value of the current name of the script. So really, $PHP_SELF will have a value of "myform.php" in this instance. When you use $PHP_SELF as a form action, you're saying "when you hit the submit button, reload this script and do something" instead of "when you hit the submit button, go find another script and do something." |
|
Now that we have a shell of a script, we need to step back and figure out what we want this script to do, since we need it to perform multiple actions. We know we need to:
1. Display the form.
2. Submit the form.
3. Check for errors.
4. Print error messages without sending the form.
5. Send form if no error. |
|
Wow. All that in one script? Not a problem. I often use a variable called "op" to determine which OPeration (get it? "op"?) to perform. The value of this variable is set in a hidden form field, and usually I call it "ds". This isn't some complicated programming abbreviation -- I made it up, and it stands for "do something". So, the first thing the script should do is look for the value of $op that equals "ds". This value will ONLY have been sent if the form has been submitted already. So, if the value of $op is not "ds", then the user hasn't seen the form and thus it should be shown. Add a hidden form field to the $form_block above, for $op, then add the following if... statement: |
|
if ($op != "ds") {
// they need to see the form
echo "$form_block";
}
|
|
|
To this point, the code should look like this: |
|
<HTML>
<HEAD>
<TITLE>Feedback</TITLE>
</HEAD>
<BODY>
<?
$form_block = "
<FORM method=\"POST\" action=\"$PHP_SELF\">
<P>Your Name:<br>
<INPUT type=\"text\" name=\"sender_name\" size=30></p>
<P>Your E-Mail Address:<br>
<INPUT type=\"text\" name=\"sender_email\" size=30></p>
<P>Your Message:<br>
<textarea name=\"message\" cols=30 rows=5></textarea>
</p>
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">
<P><INPUT type=\"submit\" value=\"Send This Form\"></p>
</FORM>
";
if ($op != "ds") {
// they need to see the form
echo "$form_block";
}
?>
</BODY>
</HTML>
|
|
|
So, now this form has four fields: one hidden field, giving a value of "ds" to $op, two text fields, called "sender_name" and "sender_email", where the user will place his or her name and email address, and finally a textarea for a longer message, called "message". |
|
After the form is submitted, $op will equal "ds". So, we continue the if...statement from above, since upon submission, it will be skipped ($op == "ds"), and now we need to add the error checking. We want all of these fields to be required, so we check for the proper value of $ds, then a value for all of the fields. The following is a verbose way of doing things, but will help you understand the process. |
|
else if ($op == "ds") {
// check each required field and create an error message.
// if any of the required fields are blank, set $send to 0.
if ($sender_name == "") {
$name_err = "
<font color=red>Please enter your name!</font><br>
";
$send = 0;
}
if ($sender_email == "") {
$email_err = "
<font color=red>Please enter your e-mail address!</font><br>
";
$send = 0;
}
if ($message == "") {
$message_err = "
<font color=red>Please enter a message!</font><br>
";
$send = 0;
}
// now do something depending value of $send
if ($send != "0") {
$headers = "From: \"$sender_name\" <$sender_email>\n";
mail("you@yourdomain.com", "Feedback", $message, $headers);
echo "<P>Mail sent!</p>";
} else if ($send == "0") {
echo "$name_err";
echo "$email_err";
echo "$message_err";
echo "$form_block";
}
}
|
|
|
Alright, that was a lot of stuff. The else if...statement will be executed if the value of $op is "ds". First, the script looks for a value in $sender_name. If it does not find a value, it puts an error message into a value called $name_err and also sets the value of $send to 0. The same sequence repeats for the other two required fields. |
|
After checking the required fields, the script will check for a value for $send -- if the value is 0, then something is amiss, and it will print the error messages plus the form again. If everything is ok and $send does not equal 0, the message will be sent and the message "Mail sent!" will be printed to the screen. |
|
Ok, you can actually place those variables anywhere you want the error messages to be displayed. Don't stick just to my way of doing things. If $name_err, $email_err or $message_err do not contain any values, then no value will be printed (amazing, eh?). Otherwise, if any or all of those variables contain the error message, those messages will be printed, as appropriate: if $sender_name contains no value, $name_err will be printed, and so forth. |
|
But wait! Wouldn't it be cool to hold the values of the fields which were actually completed? Just modify the original $form_block, so that it looks like this: |
|
$form_block = "
<FORM method=\"POST\" action=\"$PHP_SELF\">
<P>Your Name:<br>
<INPUT type=\"text\" name=\"sender_name\"
value=\"$sender_name\" size=30></p>
<P>Your E-Mail Address:<br>
<INPUT type=\"text\" name=\"sender_email\"
value=\"$sender_email\" size=30></p>
<P>Your Message:<br>
<textarea name=\"message\" cols=30
rows=5>$message</textarea>
</p>
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">
<P><INPUT type=\"submit\" value=\"Send This Form\"></p>
</FORM>
";
?>
|
|
|
From top to bottom, the myform.php script should look something like this: |
|
|
<HTML>
<HEAD>
<TITLE>Feedback</TITLE>
</HEAD>
<BODY>
<?
$form_block = "
<FORM method=\"POST\" action=\"$PHP_SELF\">
<P>Your Name:<br>
<INPUT type=\"text\" name=\"sender_name\"
value=\"$sender_name\" size=30></p>
<P>Your E-Mail Address:<br>
<INPUT type=\"text\" name=\"sender_email\"
value=\"$sender_email\" size=30></p>
<P>Your Message:<br>
<textarea name=\"message\" cols=30
rows=5>$message</textarea>
</p>
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">
<P><INPUT type=\"submit\" value=\"Send This Form\"></p>
</FORM>
";
if ($op != "ds") {
// they need to see the form
echo "$form_block";
} else if ($op == "ds") {
// check each required field and create an error message.
// if any of the required fields are blank, set $send to 0.
if ($sender_name == "") {
$name_err = "
<font color=red>Please enter your name!</font><br>
";
$send = 0;
}
if ($sender_email == "") {
$email_err = "
<font color=red>Please enter your e-mail address!</font><br>
";
$send = 0;
}
if ($message == "") {
$message_err = "
<font color=red>Please enter a message!</font><br>
";
$send = 0;
}
// now do something depending value of $send
if ($send != "0") {
$headers = "From: \"$sender_name\" <$sender_email>\n";
mail("you@yourdomain.com", "Feedback", $message, $headers);
echo "<P>Mail sent!</p>";
} else if ($send == "0") {
echo "$name_err";
echo "$email_err";
echo "$message_err";
echo "$form_block";
}
}
?>
</BODY>
</HTML>
|
|
|
| |
| Setup and Install Apache with PHP4 as a Dynamic Module (DSO) Categories : PHP, PHP Configuration, Apache | | | Setup and Install Apache and PHP4 on Windows Categories : PHP, PHP Configuration, Apache, Windows 2000 | | | Overriding the php.ini with no access! Categories : PHP, PHP Configuration | | | Working with Dates and Times in PHP Categories : PHP, Date Time | | | Creating Auto-incrementing ID Fields with PHP and Oracle Categories : PHP, PHP options/info, Databases, Oracle | | | Date Arithmetic With MySQL Categories : PHP, Databases, MySQL, Date Time | | | PHP CLI and Cron Categories : PHP, CLI, Cron | | | Honey, I Shrunk My Website Categories : PHP, PHP options/info, Site Planning, Other | | | Aspect-Oriented Programming and PHP Categories : PHP, Aspect Oriented Programming | | | Saving Images in MySQL Categories : MySQL, PHP, Graphics, Databases | | | Introduction to WAP using WML, ASP and PHP Categories : PHP, WAP, WML | | | Beginners guide to PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, Installation | | | Building XML Web Services with PHP NuSOAP Categories : PHP, NuSOAP, XML | | | Who's Linking? Categories : PHP, Beginner Guides, To PHP | | | Building a Counter Categories : PHP, Cookies | |
| |
|
|