May 1st, 2008
How to Create Feedback Form in PHP - Part II
How to do Form Validation in Feedback Form
In first part of tutorial we learnt and created a simple but useful feedback script, which transmits the contents of a feedback form to webmaster through email and today we are going to refine our feedback script by putting Form Validation and Disabling Browser Caching. Now lets start:
How to Do Form Validation
If you have ever placed a form on your website before, you will probably have received the results of submissions that were incomplete in some way. For example, the visitor submitting the form may have completely omitted his email address or entered invalid email address (abc.abcCom) Or, in the case of trigger-happy visitors, they may have accidentally hit the Submit button before even writing their comments.
To make the feedback script more robust, it is useful to have some sort of checking to ensure that all essential fields have been completed before sending the message to the webmaster. The simplest way to do this is to modify the script we wrote sendmail.php in the first part of this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php $email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ; if (!isset($_REQUEST['email'])) { header( "Location: http://www.example.com/feedback.html" ); } elseif (empty($email) || empty($message)) { echo "<center>Sorry, email or message is not entered !</center>"; exit; } else { mail( "yourname@example.com", "Feedback Form Results", $message, "From: $email" ); header( "Location: http://www.example.com/thankyou.html" ); } ?> |
If you recall, the feedback form which we created in previous tutorial looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <html> <head> <title>HeyGB.com - Feedback Form</title> </head> <body> <form method="post" action="sendmail.php"> Email: <input name="email" type="text" /><br /> Message:<br /> <textarea name="message" rows="15" cols="40"> </textarea><br /> <input type="submit" /> </form> </body> </html> |
Now lets take a deep look what of sendmail.php which we have just modified above:
The first change we’ve made is that we check if the variable $_REQUEST[’email’] has been defined by using the isset() function. In the first tutorial, I mentioned that it would have been defined if your script was called from your feedback form since PHP automatically provides your script access to all form fields through $_REQUEST[’form-field-name’] (where form-field-name is the name of the field in your form).
This check is useful to catch instances where your visitor tries to invoke “http://www.example.com/sendmail.php” just to see what happens. Without this check, you will wind up with a blank email in your mailbox if anyone do this.
This revised script checks to see if the “email” field has been set, and if it has not, it means that the visitor has called the script directly without going through your form. In such a case, the script redirects the visitor to your feedback form. The next statement:
1 | elseif (empty($email) || empty($message)) |
checks if the form was submitted without the visitor entering anything in either (or both) of those fields. The empty() function checks the variable enclosed within its brackets to see if they contain anything. If nothing is found in either of these variables, or if they have not been set, your visitor will see a error message:
Sorry, email or message is not entered !
Finally, if all is well, the form is submitted using the code explained in our previous tutorial.
Note: In sendmail.php we used three keywords: “if”, “elseif” and “else”. Like many programming languages, these control structures allow certain portions of your script to be executed only if a particular condition is true. If you don’t remember it then please have a look Learn PHP From Scratch - Part V in which we have covered control structured in detail.
Pages : 1 2

