SEARCH
  Web Programming PHP Form Validation in PHP

Form Validation in PHP

2torial Blog PHP Dec 10, 2008

form validation in PHP

Today I'll explain how to do form validation in PHP. 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:





#form {
width: 500px;
margin-right: auto;
margin-left: auto;
}
->








Enter your information in the form below.

First Name:




Last Name:




E-Mail:




Comments:











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.
";
}

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
. 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:


if (!empty($_POST['fname'])){
$msg = "fname; $_POST[fname] ";
}else{
$fname = NULL;
echo "Please fill out your first name.
";
}
if (!empty($_POST['lname'])){
$msg .= "lname: $_POST[lname] ";
}else{
$lname = NULL;
echo "Please fill out your last name.
";
}
if (!empty($_POST['email'])){
$msg .= "email: $_POST[email] ";
}else{
$email = NULL;
echo "Please leave your email address.
";
}
if (!empty($_POST['comments'])){
$msg .= "comments: $_POST[comments] ";
}else{
$comments = NULL;
echo "You forgot to leave a comment.
";
}
$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. So, now you can do form validation in PHP like a boss.

subscribe to newsletter