WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Custom Error Messages When Using $PHP_SELF as Form Action
Categories : PHP, PHP Configuration Picture not available
Meloni Julie
Date : 2000-04-24
Grade : 0 of 5 (graded 0 times)
Viewed : 16425
Search : More Articles by Meloni Julie
Action : Grade This Article
Tools : My Favotite Articles


Submit your own code examples 
 


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&lt;/TITLE>
&lt;/HEAD>
<BODY>

<?

$form_block
= "

<FORM method=\"POST\" action=\"$PHP_SELF\">

<P>Your Name:<br>
<INPUT type=\"text\" name=\"sender_name\" size=30>&lt;/p>

<P>Your E-Mail Address:<br>
<INPUT type=\"text\" name=\"sender_email\" size=30>&lt;/p>

<P>Your Message:<br>
<textarea name=\"message\" cols=30 rows=5>&lt;/textarea>
&lt;/p>

<P><INPUT type=\"submit\" value=\"Send This Form\">&lt;/p>

&lt;/FORM>

"
;

?>

&lt;/BODY>
&lt;/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&lt;/TITLE>
&lt;/HEAD>
<BODY>

<?

$form_block
= "

<FORM method=\"POST\" action=\"$PHP_SELF\">

<P>Your Name:<br>
<INPUT type=\"text\" name=\"sender_name\" size=30>&lt;/p>

<P>Your E-Mail Address:<br>
<INPUT type=\"text\" name=\"sender_email\" size=30>&lt;/p>

<P>Your Message:<br>
<textarea name=\"message\" cols=30 rows=5>&lt;/textarea>
&lt;/p>

<INPUT type=\"hidden\" name=\"op\" value=\"ds\">


<P><INPUT type=\"submit\" value=\"Send This Form\">&lt;/p>

&lt;/FORM>

"
;

if (
$op != "ds") {

// they need to see the form

echo "$form_block";

}

?>


&lt;/BODY>
&lt;/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!&lt;/font><br>
";
$send = 0;

}

if ($sender_email == "") {

$email_err = "
<font color=red>Please enter your e-mail address!&lt;/font><br>
";
$send = 0;

}

if ($message == "") {

$message_err = "
<font color=red>Please enter a message!&lt;/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!&lt;/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>&lt;/p>

<P>Your E-Mail Address:<br>
<INPUT type=\"text\" name=\"sender_email\"
value=\"$sender_email\" size=30>&lt;/p>

<P>Your Message:<br>
<textarea name=\"message\" cols=30
rows=5>$message&lt;/textarea>
&lt;/p>

<INPUT type=\"hidden\" name=\"op\" value=\"ds\">

<P><INPUT type=\"submit\" value=\"Send This Form\">&lt;/p>

&lt;/FORM>

";

?>


From top to bottom, the myform.php script should look something like this:





<HTML>
<HEAD>
<TITLE>Feedback&lt;/TITLE>
&lt;/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>&lt;/p>

<P>Your E-Mail Address:<br>
<INPUT type=\"text\" name=\"sender_email\"
value=\"$sender_email\" size=30>&lt;/p>

<P>Your Message:<br>
<textarea name=\"message\" cols=30
rows=5>$message&lt;/textarea>
&lt;/p>

<INPUT type=\"hidden\" name=\"op\" value=\"ds\">


<P><INPUT type=\"submit\" value=\"Send This Form\">&lt;/p>

&lt;/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!&lt;/font><br>
"
;
$send = 0;

}

if (
$sender_email == "") {

$email_err = "
<font color=red>Please enter your e-mail address!&lt;/font><br>
"
;
$send = 0;

}

if (
$message == "") {

$message_err = "
<font color=red>Please enter a message!&lt;/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!&lt;/p>";

} else if (
$send == "0") {

echo
"$name_err";
echo
"$email_err";
echo
"$message_err";
echo
"$form_block";

}


}

?>

&lt;/BODY>
&lt;/HTML>








Overriding the php.ini with no access!
Categories : PHP, PHP Configuration
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
PHP 101 Part 2 of 15 : Calling All Operators
Categories : PHP, Beginner Guides, Operators
Working with Dates and Times in PHP
Categories : PHP, Date Time
Generating Images on the Fly With PHP
Categories : PHP, Graphics, GD image library
Alternating row colors with PHP and mySQL
Categories : PHP, Databases, MySQL, HTML and PHP
Simple Form-Based File Uploads
Categories : PHP, File System
Installing PHP Under Personal Web Server
Categories : Personal Web Server (PWS), PHP, Web Servers, Installation
Web database Information Flow
Categories : PHP, MySQL
Building Cross Platform GUI Apps With PHP-GTK
Categories : PHP, PHP-GTK, GUI Apps
Using PEAR HTML_Template_IT For Modular Interface Design
Categories : PHP, PEAR, Templates
Multicolumn Output from a Database with PHP
Categories : PHP, Databases, HTML and PHP, MySQL
Jump Start to Easy URLs
Categories : PHP, Beginner Guides, MySQL, File System, To PHP
Injecting XML Content Into Page Templates With patXMLRenderer (part 1)
Categories : PHP, XML, Templates