PHP Form Validation Technique

3,229 views 25 slides Sep 22, 2016
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

In this tutorial, the detail about PHP Form Validation technique is discussed with a practical example and proper explanation.


Slide Content

PHP Form Validation Technique Presented by- Morshedul Arefin www.arefintuts.com

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 Overview www.arefintuts.com

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 … Why Validation Is Needed www.arefintuts.com

A Sample Form Where we will use the validation HTML Section for 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

We have added total five text fields for Name, Email Address, Username, Password and Re-type Password sections. Text Fields 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">

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. Radio Buttons www.arefintuts.com <input type="radio" name="u_gender"> Male <input type="radio" name="u_gender"> Female

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. Select List 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>

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. Checkbox www.arefintuts.com <input type="checkbox" name="u_term">

Biography of a user is represented using the textarea, because this section may contain multiple lines as input. Textarea www.arefintuts.com <textarea name="u_bio" cols="30" rows="10"></textarea>

Finally, We have added a submit button for the submission of form data into the same page and see that button below: Submit Button www.arefintuts.com <input type="submit" value="SUBMIT">

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: Passing Form Data to the Same Page 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.

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: Checking the Top of Page www.arefintuts.com if ($_SERVER["REQUEST_METHOD"] == "POST")

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 (you can use any value, 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. Concept of Validation www.arefintuts.com

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: Validating Name, Gender, Country, Bio and Term 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>"; }

Validating Name, Gender, Country, Bio and Term… 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

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: Validating Email Address 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>"; } }

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: Validating Username www.arefintuts.com

Validating Username… 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

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: Validating Password www.arefintuts.com

Validating Password… 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>"; } }

When the value of previously discussed $valid variable is not changed, you can generate a success message like this: Checking Final Validation www.arefintuts.com if($valid == 1) { $success_message = "Everything is OK"; }

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: Error and Success Message 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>'; }

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/ Complete Code (Source Code) www.arefintuts.com

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 Conclusion www.arefintuts.com

Our website: http://www.arefintuts.com Email: [email protected] Facebook : https://www.facebook.com/arefintuts / THANK TOU Contact Us www.arefintuts.com