2. Objective
Learning the Form Validation Technique using the PHP programming
language
Requirements
Understanding Basic PHP
Understanding Basic HTML and CSS codes
Benefits for you (What you will get from this slide)
Text Tutorial with step by step explanation
Source Code
Comment Section Link
www.arefintuts.com
3. Reason for Validation
To protect the website from illegal user input
To protect the website from bad people
Types of Forms where Validation is Important
Sign up Form
Sign in Form
Contact Form
Request a Quote Form
Enquiry Form
And many more …
www.arefintuts.com
4. A Sample Form Where we will use the validation
www.arefintuts.com
If you can not properly view the
photo or photo becomes small,
Then please visit the following
link to see the photo perfectly:
http://www.arefintuts.com/wp-content/uploads/php-form-validation.png
5. We have added total five text fields for Name, Email Address,
Username, Password and Re-type Password sections.
www.arefintuts.com
<input type="text" name="u_name">
<input type="text" name="u_email">
<input type="text" name="u_username">
<input type="password" name="u_password">
<input type="password" name="u_re_password">
6. We have used two radio buttons for Gender section. Here you have
to be careful that both the “name” properties should have the same
value; otherwise it will not properly.
www.arefintuts.com
<input type="radio" name="u_gender"> Male
<input type="radio" name="u_gender"> Female
7. We have inserted a select list for Country section, and there are total
three countries there. This is just an example. You can add as more
countries as you need when you will work in practical case.
www.arefintuts.com
<select name="u_country">
<option value="">Select a Country</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
<option value="Bangladesh">Bangladesh</option>
</select>
8. In the bottom of page a checkbox is inserted in order to check the
terms. If user does not click on the checkbox for the terms, he or she
will not be able to proceed to the next page.
www.arefintuts.com
<input type="checkbox" name="u_term">
9. Biography of a user is represented using the textarea, because this
section may contain multiple lines as input.
www.arefintuts.com
<textarea name="u_bio" cols="30" rows="10"></textarea>
10. Finally, We have added a submit button for the submission of form
data into the same page and see that button below:
www.arefintuts.com
<input type="submit" value="SUBMIT">
11. After giving input to the form fields, you can pass the data to the same
page or another separate page. In this case, we will pass data into the
same page. To do this, just write the <form> tag as below:
www.arefintuts.com
<form action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
Here the superglobal variable $_SERVER is used and its parameter
PHP_SELF indicates that the form data is passed in the same page.
12. In the beginning of the page, we will check if any form data is
submitted or not. If data is submitted, we will check for validation.
Otherwise, we will just proceed to show the fields normally. In order to
check that, we will run the follow conditional statement:
www.arefintuts.com
if ($_SERVER["REQUEST_METHOD"] == "POST")
13. Our concept in the validation process is very simple. We have created
a variable $valid and set up its value as 1 initially. If no validation
error occurs, this value will never be changed, and we will print the
success message. But if this value is changed to 0 (you can use any
value, 0 is used just as an instance) anyhow in the validation process,
we will finally stop the script so that it can’t proceed further and put
the specific error message.
www.arefintuts.com
14. In our process, validating the Name, Gender, Country, Bio and Term is
very easy. Just we need to check if user has given data to those fields
or not. The codes for validating these fields are shown here:
www.arefintuts.com
if(empty($_POST['u_name'])) {
$valid = 0;
$error_message .= "Name can not be empty <br>";
}
if(empty($_POST['u_gender'])) {
$valid = 0;
$error_message .= "You must have to select a gender <br>";
}
15. www.arefintuts.com
if(empty($_POST['u_country'])) {
$valid = 0;
$error_message .= "You must have to select a country <br>";
}
if(empty($_POST['u_bio'])) {
$valid = 0;
$error_message .= "Bio can not be empty <br>";
}
if(empty($_POST['u_term'])) {
$valid = 0;
$error_message .= "You must have to agree with the terms
<br>";
}
Check the full code from pastebin:
http://pastebin.com/4sEKVi3b
16. Email address validation is a bit tricky. First of all, you will have to check if user
has given any email address into the text field or not. If user gives something
in the input field, now we will have to again check if that format is a valid email
address or not. Therefore, here is 2 parts. The first part is same as before. But
in the second part, we will use a filter that is called
FILTER_VALIDATE_EMAIL. You can use some other alternate methods too
like preg_match or manually coding for validation. Here are the codes we
used:
www.arefintuts.com
if(empty($_POST['u_email'])) {
$valid = 0;
$error_message .= "Email Address can not be empty <br>";
} else {
if(filter_var($_POST['u_email'], FILTER_VALIDATE_EMAIL) ===
false) {
$valid = 0;
$error_message .= "Email Address is invalid <br>";
}
}
17. Validating username is easy also. As a test case, we have setup a rule
here. Username must be at least 5 characters in long and must
contain at least 1 number and 1 alphabetic character. This rule will
vary project to project. First of all, the compiler will check if there is
any empty data or not. If the input field is not empty (it means user
has given something in the text box), then username will be checked
for validation. We have used 2 built-in functions of PHP here: strlen
and preg_match_all. The first function strlen will check the total length
of string that user gives as input and the second function
preg_match_all will check how many letters and alphabets the string
contains. The code is something like this:
www.arefintuts.com
18. www.arefintuts.com
if(empty($_POST['u_username'])) {
$valid = 0;
$error_message .= "Username can not be empty <br>";
} else {
$len = strlen($_POST['u_username']);
if($len < 5) {
$valid = 0;
$error_message .= "Username must be at least 5 characters in long
<br>";
} else {
$count_number = preg_match_all( "/[0-9]/", $_POST['u_username'] );
$count_alphabet = preg_match_all( "/[A-Za-z]/", $_POST['u_username'] );
if( $count_number == 0 || $count_alphabet == 0 ) {
$valid = 0;
$error_message .= "Username must contain at least 1 number and 1
alphabetic character <br>";
}
}
}
If you feel problem to see the above code, see the code
From pastebin: http://pastebin.com/c3FmKmtu
19. Password can be validated in multiple ways. Here we do not use a
complex rule for password. Our rule is very simple. First, compiler will
check if data is given into the password and retype password fields. If
data is given there, compiler will match both of those. The code will be
like this:
www.arefintuts.com
20. www.arefintuts.com
if(empty($_POST['u_password'])) {
$valid = 0;
$error_message .= "Password can not be empty <br>";
}
if(empty($_POST['u_re_password'])) {
$valid = 0;
$error_message .= "Retype Password can not be empty <br>";
}
if(!empty($_POST['u_password']) && !empty($_POST['u_re_password']))
{
if($_POST['u_password'] != $_POST['u_re_password']) {
$valid = 0;
$error_message .= "Passwords do not match <br>";
}
}
21. When the value of previously discussed $valid variable is not
changed, you can generate a success message like this:
www.arefintuts.com
if($valid == 1) {
$success_message = "Everything is OK";
}
22. We did not print error and success message in the point of errors. We
just kept all into variables. We will print the messages just before the
form starts. You can change the location of these messages into
anywhere in your PHP page. See how the code looks like:
www.arefintuts.com
if($error_message != '') {
echo '<div class="red">'.$error_message.'</div><br>';
}
if($success_message != '') {
echo '<div class="green">'.$success_message.'</div><br>';
}
23. In this php form validation system, I have given my file name as
form-validation.php. You can download the source code from here:
http://www.arefintuts.com/php-form-validation-technique/
www.arefintuts.com
24. Many people can do PHP form validation in different ways. In this
tutorial, we have given a PHP form example just to show you the total
process and concept of validation. Since you are a programmer you
can do it your own ways.
If you fail to understand anything about form validation in PHP, don’t
hesitate to ask me in the comment section of this page:
http://www.arefintuts.com/php-form-validation-technique/
THANK TOU
www.arefintuts.com