Today I'll take it up a level and re-write that script using the if, else conditional and empty() function to include form validation.
Using an if, else conditional: if (condition) {do something} else { do this} , we can make certain fields of our form required that they be filled out. Let's start by making a basic form with first name, last name, email and comments as our text inputs. Copy the following code into a text document then save and name it form.html:
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8″ /> <title>Feedback Form</title> <style type="text/css"> <!- #form { width: 500px; margin-right: auto; margin-left: auto; } -> </style> </head> <body> <div id="form"> <form action="sendmail.php" method="post"> <fieldset><legend>Enter your information in the form below.</legend><br /> First Name:<br/> <input type="text" name="fname" size="20" maxlength="40" /><br /><br /> Last Name:<br/> <input type="text" name="lname" size="40" maxlength="60"/><br /><br /> E-Mail:<br /> <input type="text" name="email" size="40" maxlength="60"/><br /><br /> Comments:<br /> <textarea name="comments" cols="40" rows="7"/></textarea> </fieldset> <div align="center"><input type="reset" /> <input type="submit" name="submit" value="Submit my information" /></div> </form> </div> </body> </html> |
The action for the form is the same as the last form tutorial (send.php) but we'll re-write it now to include the changes. The easiest way to check to see if a user has typed in a value in the text boxes is to use the empty() function. Using the old send.php the code looked like this for the Name variable: $msg = "Name; $_POST[name]"; . Using the empty() function and an if, else conditional to make sure the user typed in a value for the "first name" text box, the revised script is written like this:
| if (!empty($_POST['fname'])){ $msg = "fname; $_POST[fname]"; }else{ $fname = NULL; echo "Please fill out your first name.<br />"; } |
In the above script the empty() function checks to see if the "first name" box has a value. If yes, the value of the $fname variable would be sent to the recipients email. If the box is empty the $fname variable is set to NULL and the user sees the message "please fill out your first name" followed by a line break <br />. The "" that you see in the string simply adds a line break to the information being sent so that when the recipient receives the email, its not one continuous line.
So, the entire PHP script to check all text inputs is:
| <?php if (!empty($_POST['fname'])){ $msg = "fname; $_POST[fname]"; }else{ $fname = NULL; echo "Please fill out your first name.<br />"; } if (!empty($_POST['lname'])){ $msg .= "lname: $_POST[lname]"; }else{ $lname = NULL; echo "Please fill out your last name.<br />"; } if (!empty($_POST['email'])){ $msg .= "email: $_POST[email]"; }else{ $email = NULL; echo "Please leave your email address.<br />"; } if (!empty($_POST['comments'])){ $msg .= "comments: $_POST[comments]"; }else{ $comments = NULL; echo "You forgot to leave a comment.<br />"; } $recipient = "yourname@yourdomain.com"; $subject = "Form Feedback"; $mailheaders = "Reply-to: $_POST[email]"; //send the mail mail($recipient, $subject, $msg, $mailheaders); ?> |
Put this between the body tags of a new html document (make sure to change the $recipient to your email address) then save and name it send.php . Style the form.html to match your website then upload both the form.html and send.php to your server in the same directory you keep all of your other web site pages, and your done.
Related Articles
- Why is Validation Important?Validation in your webpages is very important as proven in this tutorial. In this tutorial youll learn about Validation and how you should be making the effort to get validated!
- PHP:Form Series: Validators & Client-side ValidationThis tutorial is really helpful to those of us who are a little intimidated by PHP:form. All of those hard to answer questions are easily explained in this simple to use educational web page. Hope you enjoy!
- Form Validation using PHPToday Ill take it up a level and re-write that script using the if, else conditional and empty() function to include form validation.
- HTML Forms Validation On The Client SideForm validation on the client-side is essential - it saves time and bandwidth; you also have better control to show the user the wrong field. This comparison of different validation methods includes comments for improving portability and maintainability.
- CSS Validation and HTMLHTML validation and CSS validation are controversial issues with some people. This article discusses some of the issues that have come increasingly to the fore in web development. The article will also provide a practical method that overworked webmasters can use to improve their website.
- Why do you Need a Validated Website?This is a very good article for new designers or designers who are big on the visuals but not the programming. Why do you Need a Validated Website? - the author explains it really well.
- Code Validation & Compliancy - The New Beginning XHTMLCode validation is still widely debated as to whether it is required for performance within the search engines. It is only a guess that the search engines dont utilise it within their algorithm, but nobody is actually 100% sure on that fact...
- 7 Benefits of HTML ValidationYou may not bother with html validation or writing simple and clean code when designing your web site. Later you may find your site is slow loading, appears incorrectly in the main browsers and does not rank well for the major search engines...
- E-mail Validation with PHPWhen you register on at a website, the site normally checks if the e-mail address that you enter is in a valid format. This is done by using what called a Regular Expression. What we need to do is check if a string (eg $email) match...
- What does W3C Validation Stand For?This article will explain this to you if your website is W3C Validated and what W3C stands for and means.
