SlideShare a Scribd company logo
1 of 25
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
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 …
www.arefintuts.com
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
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">
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
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>
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">
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>
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">
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.
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")
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
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>";
}
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:
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:
www.arefintuts.com
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:
www.arefintuts.com
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:
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:
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/
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
www.arefintuts.com
 Our website: http://www.arefintuts.com
 Email: arefin2k@gmail.com
 Facebook: https://www.facebook.com/arefintuts/
THANK TOU
www.arefintuts.com

More Related Content

What's hot (20)

Practical file on web technology(html)
Practical file on web technology(html)Practical file on web technology(html)
Practical file on web technology(html)
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Java script
Java scriptJava script
Java script
 
Html presentation
Html presentationHtml presentation
Html presentation
 
Operators
OperatorsOperators
Operators
 
An Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java ScriptAn Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java Script
 
HTML: Tables and Forms
HTML: Tables and FormsHTML: Tables and Forms
HTML: Tables and Forms
 
HTML CSS & Javascript
HTML CSS & JavascriptHTML CSS & Javascript
HTML CSS & Javascript
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Introduction of Html/css/js
Introduction of Html/css/jsIntroduction of Html/css/js
Introduction of Html/css/js
 
Cascading Style Sheet (CSS)
Cascading Style Sheet (CSS)Cascading Style Sheet (CSS)
Cascading Style Sheet (CSS)
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
 
Form validation client side
Form validation client side Form validation client side
Form validation client side
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
HTML CSS Basics
HTML CSS BasicsHTML CSS Basics
HTML CSS Basics
 
Html basics
Html basicsHtml basics
Html basics
 
Php forms
Php formsPhp forms
Php forms
 
Js ppt
Js pptJs ppt
Js ppt
 
Javascript
JavascriptJavascript
Javascript
 

Similar to PHP Form Validation Technique

Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiNaveen Kumar Veligeti
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptxSherinRappai
 
Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validationH K
 
Angular js form validation shashi-19-7-16
Angular js form validation shashi-19-7-16Angular js form validation shashi-19-7-16
Angular js form validation shashi-19-7-16Shashikant Bhongale
 
full stack practical assignment msc cs.pdf
full stack practical assignment msc cs.pdffull stack practical assignment msc cs.pdf
full stack practical assignment msc cs.pdfHulkTheDevil
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In PhpHarit Kothari
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScriptRavi Bhadauria
 
User authentication module using php
User authentication module using phpUser authentication module using php
User authentication module using phpRishabh Srivastava
 
Php Security3895
Php Security3895Php Security3895
Php Security3895Aung Khant
 
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5Salvatore Iaconesi
 
12-security.ppt - PHP and Arabic Language - Index
12-security.ppt - PHP and Arabic Language - Index12-security.ppt - PHP and Arabic Language - Index
12-security.ppt - PHP and Arabic Language - Indexwebhostingguy
 
The Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsThe Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsVincent Chien
 
Web Security Mistakes: Trusting The Client
Web Security Mistakes: Trusting The ClientWeb Security Mistakes: Trusting The Client
Web Security Mistakes: Trusting The Clientgrutz
 
Html advanced-reference-guide for creating web forms
Html advanced-reference-guide for creating web formsHtml advanced-reference-guide for creating web forms
Html advanced-reference-guide for creating web formssatish 486
 

Similar to PHP Form Validation Technique (20)

forms.pptx
forms.pptxforms.pptx
forms.pptx
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
 
Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validation
 
Html forms
Html formsHtml forms
Html forms
 
8.pdf
8.pdf8.pdf
8.pdf
 
Angular js form validation shashi-19-7-16
Angular js form validation shashi-19-7-16Angular js form validation shashi-19-7-16
Angular js form validation shashi-19-7-16
 
full stack practical assignment msc cs.pdf
full stack practical assignment msc cs.pdffull stack practical assignment msc cs.pdf
full stack practical assignment msc cs.pdf
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
 
Php security3895
Php security3895Php security3895
Php security3895
 
PHP Security
PHP SecurityPHP Security
PHP Security
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
 
User authentication module using php
User authentication module using phpUser authentication module using php
User authentication module using php
 
Php Security3895
Php Security3895Php Security3895
Php Security3895
 
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
 
12-security.ppt - PHP and Arabic Language - Index
12-security.ppt - PHP and Arabic Language - Index12-security.ppt - PHP and Arabic Language - Index
12-security.ppt - PHP and Arabic Language - Index
 
Security.ppt
Security.pptSecurity.ppt
Security.ppt
 
The Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsThe Django Book - Chapter 7 forms
The Django Book - Chapter 7 forms
 
Web Security Mistakes: Trusting The Client
Web Security Mistakes: Trusting The ClientWeb Security Mistakes: Trusting The Client
Web Security Mistakes: Trusting The Client
 
Html advanced-reference-guide for creating web forms
Html advanced-reference-guide for creating web formsHtml advanced-reference-guide for creating web forms
Html advanced-reference-guide for creating web forms
 

Recently uploaded

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformWSO2
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governanceWSO2
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityVictorSzoltysek
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 

Recently uploaded (20)

Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 

PHP Form Validation Technique

  • 1. Presented by- Morshedul Arefin www.arefintuts.com
  • 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
  • 25.  Our website: http://www.arefintuts.com  Email: arefin2k@gmail.com  Facebook: https://www.facebook.com/arefintuts/ THANK TOU www.arefintuts.com