SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
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
Naveen Kumar Veligeti
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
Jalpesh Vasa
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
Nisa Soomro
 
Php forms
Php formsPhp forms
Php forms
Anne Lee
 
php
phpphp
php
ajeetjhajharia
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Jquery
JqueryJquery
Jquery
Girish Srivastava
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
baabtra.com - No. 1 supplier of quality freshers
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
Html form tag
Html form tagHtml form tag
Html form tag
shreyachougule
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
Santhiya Grace
 
Form using html and java script validation
Form using html and java script validationForm using html and java script validation
Form using html and java script validation
Maitree Patel
 
Php array
Php arrayPhp array
Php array
Nikul Shah
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
Spy Seat
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
Jussi Pohjolainen
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
WordPress Memphis
 

Similar to PHP Form Validation Technique (20)

forms.pptx
forms.pptxforms.pptx
forms.pptx
asmabagersh
 
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldkskUnit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
cpbloger553
 
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldkskUnit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
cpbloger553
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
SherinRappai
 
Working with Data and built-in functions of PHP
Working with Data and built-in functions of PHPWorking with Data and built-in functions of PHP
Working with Data and built-in functions of PHP
mohanaps
 
Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validation
H K
 
Html forms
Html formsHtml forms
Html forms
nobel mujuji
 
8.pdf
8.pdf8.pdf
8.pdf
JohnnielMar1
 
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
Shashikant 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.pdf
HulkTheDevil
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
Harit Kothari
 
Php security3895
Php security3895Php security3895
Php security3895
PrinceGuru MS
 
PHP Security
PHP SecurityPHP Security
PHP Security
manugoel2003
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
Ravi Bhadauria
 
User authentication module using php
User authentication module using phpUser authentication module using php
User authentication module using php
Rishabh Srivastava
 
Php Security3895
Php Security3895Php Security3895
Php Security3895
Aung 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 5
Salvatore 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 - Index
webhostingguy
 
Security.ppt
Security.pptSecurity.ppt
Security.ppt
webhostingguy
 
The Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsThe Django Book - Chapter 7 forms
The Django Book - Chapter 7 forms
Vincent Chien
 
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldkskUnit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
cpbloger553
 
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldkskUnit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
cpbloger553
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
SherinRappai
 
Working with Data and built-in functions of PHP
Working with Data and built-in functions of PHPWorking with Data and built-in functions of PHP
Working with Data and built-in functions of PHP
mohanaps
 
Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validation
H 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-16
Shashikant 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.pdf
HulkTheDevil
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
Harit Kothari
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
Ravi Bhadauria
 
User authentication module using php
User authentication module using phpUser authentication module using php
User authentication module using php
Rishabh Srivastava
 
Php Security3895
Php Security3895Php Security3895
Php Security3895
Aung 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 5
Salvatore 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 - Index
webhostingguy
 
The Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsThe Django Book - Chapter 7 forms
The Django Book - Chapter 7 forms
Vincent Chien
 
Ad

Recently uploaded (20)

Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Ad

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: [email protected]  Facebook: https://www.facebook.com/arefintuts/ THANK TOU www.arefintuts.com