SlideShare a Scribd company logo
PHP Forms
 GET and POST Method
 Form Validation
 Form Required Fields
*Property of STI K0032
GET and POST Method
ο‚ A form data can be submitted using POST
and GET method
ο‚ Both are used for same purpose but stand
apart for some specifications
ο‚ GET and POST create an array which holds
key/value pairs, where keys are the name of
the form controls and values are the input
data by the user
*Property of STI K0032
GET and POST Method
ο‚ Both GET and POST method are treated as
$_GET and $_POST in PHP
ο‚ These methods are superglobals, which
means that they are always accessible, and
they can be accessed using any function,
class or file
ο‚ The $_GET method is an associative array of
variables passed to the current script via the
URL parameters
*Property of STI K0032
GET and POST Method
ο‚ The $_POST method is an array of variables
passed to the current script via the HTTP
POST method
ο‚ In this method the information is
transferred in a hidden manner
ο‚ A form that submits information is
appended to the URL in the form of Query
String which consists of name = value pairs
in URL known as URL Encoding
*Property of STI K0032
GET and POST Method
ο‚ This string contains user values/data, which
are joined using equal (=) signs, separated
by ampersand (&), and spaces are removed
and replaced with plus (+) sign
Name1=value1&name2=value2&name3=value3
*Property of STI K0032
Get Method
ο‚  http:/.www.example.com/index.html?
name=mel@email.com&contact=09176543210
ο‚  The code below is a client-side HTML form using
method=β€œget” for user to fill the information
*Property of STI K0032
Get Method
ο‚  The code below is the server-side PHP script
where, $_GET associative array is used to receive
sent information from server end
*Property of STI K0032
Post Method
<form action="#" method="post">
....
</form>
Below is a server-side PHP script where $_POST associative
array is used to receive sent information at server end
*Property of STI K0032
Form validation
*Property of STI K0032
Form validation
The form shown in Figure 6.1 consists of the
following elements:
ο‚  Name (required field - must contain letters and
whitespaces)
ο‚  E-mail (required field - must contain valid email address)
ο‚  Website (optional field - if present, must contain valid
website URL)
ο‚  Comments (optional field - a multi-line text field)
ο‚  Gender (required field - must select a radio button )
*Property of STI K0032
Form Elements
ο‚ The Name, E-mail, Website are input
elements
ο‚ Input elements, in particular, used text and
submit values for its types attribute in order
to create text fields and buttons
ο‚ The HTML code:
*Property of STI K0032
Form Elements
ο‚ Radio button shows several options to the
users from which the user may select one
ο‚ HTML Code:
*Property of STI K0032
Form Elements
ο‚ The text area is typically a large text field
with multiple rows
ο‚ The textarea element has three attributes –
name, rows, and cols attribute
ο‚ HTML code:
*Property of STI K0032
Form Elements
ο‚  list element offers options from which the user might
choose. A list can be created using the select element,
within which is nested option elements for each option to
appear
ο‚  The select element has a name attribute giving the name
for the browser to use when identifying the selection
when the form is submitted
ο‚  The option element has a value attribute for specifying
what value to send when that option is selected, and it has
a select attribute which allows the HTML to specify which
option is initially selected. The code
*Property of STI K0032
Form Elements
ο‚  HTML Code:
*Property of STI K0032
Form Element
ο‚  The HTML code of the form element:
ο‚  when the form is submitted, the form data is sent with
method=”post”
ο‚  So, the $_SERVER["PHP_SELF"] sends the submitted
forms data to the page itself, instead of jumping to a
different page
*Property of STI K0032
Form Element
ο‚  The $_SERVER["PHP_SELF"] is a super global
variable that returns the filename of the currently
executing script
ο‚  Htmlspecialchars() function converts special
characters to HTML entities
ο‚  Cross-site scripting (XSS) is a type of computer
security vulnerability typically found inWeb
application
*Property of STI K0032
Form Element
ο‚  Example: test_form.php
ο‚  if a user enters the normal URL in the address bar like
"http://www.example.com/test_form.php", the above code
will be translated to:
*Property of STI K0032
Form Element
ο‚  consider that if a user enters the following URL in
the address bar:
http://www.example.com/test_form.php/%22%3E
%3Cscript%3Ealert('hacked')%3C/script%3E
ο‚  will be translated to:
*Property of STI K0032
Form Element
be aware that any JavaScript code can
be added inside the <script> tag
A hacker can redirect the user to a file on
another server, and that file can hold
malicious code that can alter the global
variables or submit the form to another
address to save the user’s data
*Property of STI K0032
how to avoid $_SERVER[β€œPHP_SELF”] exploit?
ο‚ The $_SERVER[β€œPHP_SELF”] exploit can be
avoided using the htmlspecialchars()
function
ο‚ if the user tries to exploit the PHP_SELF
variable, it will result:
*Property of STI K0032
Validate Form Data with PHP
ο‚  The very first thing to do to validate form data with PHP is
to pass all variables through PHP’s htmlspecialchars()
function
ο‚  For example:
ο‚  With htmlspecialchars() function it would not be executed,
because it would be saved as HTML escaped code like this:
*Property of STI K0032
ο‚ test_input()
*Property of STI K0032
Form Required Fields
ο‚  In the previous slide, all input fields were optional,
meaning no required fields to be filled in by the
user
ο‚  Here is a simple PHP script that checks the name
for empty input and throws an error message if
the input is empty:
*Property of STI K0032
Form Required Fields
To display the error message in the HTML form (this
will be generated if the user tries to submit the form
without filling in the required fields) use the code
below:

More Related Content

PPTX
Form Handling using PHP
PPTX
PHP Presentation
PDF
Php Tutorials for Beginners
PPT
PHP variables
PPTX
PHP Form Validation Technique
PDF
4.2 PHP Function
PPT
Class and Objects in PHP
PDF
Php introduction
Form Handling using PHP
PHP Presentation
Php Tutorials for Beginners
PHP variables
PHP Form Validation Technique
4.2 PHP Function
Class and Objects in PHP
Php introduction

What's hot (20)

PPT
PHP - Introduction to PHP Forms
PPT
Oops concepts in php
PPT
Php with MYSQL Database
PPTX
Php.ppt
PPT
Introduction to JavaScript (1).ppt
PPTX
Event In JavaScript
PPTX
Form using html and java script validation
PPTX
Php string function
PPTX
Statements and Conditions in PHP
PPT
Class 5 - PHP Strings
PPTX
Database Connectivity in PHP
PDF
jQuery for beginners
PDF
Web Development Course: PHP lecture 1
ODP
Introduction of Html/css/js
PPT
Introduction to XML
PPT
Introduction To PHP
PPT
PPT
PHP - Introduction to PHP AJAX
PHP - Introduction to PHP Forms
Oops concepts in php
Php with MYSQL Database
Php.ppt
Introduction to JavaScript (1).ppt
Event In JavaScript
Form using html and java script validation
Php string function
Statements and Conditions in PHP
Class 5 - PHP Strings
Database Connectivity in PHP
jQuery for beginners
Web Development Course: PHP lecture 1
Introduction of Html/css/js
Introduction to XML
Introduction To PHP
PHP - Introduction to PHP AJAX
Ad

Viewers also liked (20)

PPTX
3 php forms
PPT
Chapter 07 php forms handling
ODP
Form Processing In Php
PPTX
Php Form
Β 
PDF
Making web forms using php
PPTX
DOC
Creating a Simple PHP and MySQL-Based Login System
PDF
Forms and Databases in PHP
PPT
MySql slides (ppt)
PDF
Login and Registration form using oop in php
PDF
PHP Making Web Forms
DOCX
Php forms and validations by naveen kumar veligeti
PDF
Deliver Files With PHP
DOCX
Script login form php
PDF
03 the htm_lforms
Β 
PPT
Using arrays with PHP for forms and storing information
PPT
Geek Austin PHP Class - Session 4
DOC
S.G.Balaji Resume
PPTX
3 php forms
Chapter 07 php forms handling
Form Processing In Php
Php Form
Β 
Making web forms using php
Creating a Simple PHP and MySQL-Based Login System
Forms and Databases in PHP
MySql slides (ppt)
Login and Registration form using oop in php
PHP Making Web Forms
Php forms and validations by naveen kumar veligeti
Deliver Files With PHP
Script login form php
03 the htm_lforms
Β 
Using arrays with PHP for forms and storing information
Geek Austin PHP Class - Session 4
S.G.Balaji Resume
Ad

Similar to Php forms (20)

PPTX
5. Formshcfsjhfajkjsfjsjfjksafjsfjkjfhjsafjsajkgfjskafkjas.pptx
PPTX
Working with data.pptx
PPTX
2-Chapter Edit.pptx debret tabour university
PPTX
forms.pptx
PPTX
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
PPTX
Web Application Development using PHP Chapter 5
PDF
web2_lec6.pdf
PDF
Web app development_php_07
PPTX
PHP Form Handling
PPTX
Php and web forms
PPT
Lecture7 form processing by okello erick
PPTX
PHP Forms PHP 05
PPTX
HNDIT1022 Week 03 Part 2 Theory information.pptx
PPT
Html forms
PPT
Handling User Input and Processing Form Data
PDF
Form handling in php
PPT
Web forms and html lecture Number 4
PPTX
Web Techniques like Cookies and Sessions
PDF
Send, pass, get variables with php, form, html & java script code
PPTX
Chapter 6 Getting Data from the Client (1).pptx
5. Formshcfsjhfajkjsfjsjfjksafjsfjkjfhjsafjsajkgfjskafkjas.pptx
Working with data.pptx
2-Chapter Edit.pptx debret tabour university
forms.pptx
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
Web Application Development using PHP Chapter 5
web2_lec6.pdf
Web app development_php_07
PHP Form Handling
Php and web forms
Lecture7 form processing by okello erick
PHP Forms PHP 05
HNDIT1022 Week 03 Part 2 Theory information.pptx
Html forms
Handling User Input and Processing Form Data
Form handling in php
Web forms and html lecture Number 4
Web Techniques like Cookies and Sessions
Send, pass, get variables with php, form, html & java script code
Chapter 6 Getting Data from the Client (1).pptx

More from Anne Lee (20)

PDF
Week 17 slides 1 7 multidimensional, parallel, and distributed database
PDF
Data mining
PDF
Data warehousing
PDF
Database backup and recovery
PDF
Database monitoring and performance management
PDF
transportation and assignment models
PDF
Database Security Slide Handout
PDF
Database Security Handout
PDF
Database Security - IG
PDF
03 laboratory exercise 1 - WORKING WITH CTE
PDF
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES
PDF
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION
DOCX
Indexes - INSTRUCTOR'S GUIDE
PDF
07 ohp slides 1 - INDEXES
PDF
07 ohp slide handout 1 - INDEXES
PDF
Wk 16 ses 43 45 makrong kasanayan sa pagsusulat
PDF
Wk 15 ses 40 42 makrong kasanayan sa pagbabasa
PDF
Wk 13 ses 35 37 makrong kasanayan sa pagsasalita
PDF
Wk 12 ses 32 34 makrong kasanayan sa pakikinig
PDF
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1
Week 17 slides 1 7 multidimensional, parallel, and distributed database
Data mining
Data warehousing
Database backup and recovery
Database monitoring and performance management
transportation and assignment models
Database Security Slide Handout
Database Security Handout
Database Security - IG
03 laboratory exercise 1 - WORKING WITH CTE
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION
Indexes - INSTRUCTOR'S GUIDE
07 ohp slides 1 - INDEXES
07 ohp slide handout 1 - INDEXES
Wk 16 ses 43 45 makrong kasanayan sa pagsusulat
Wk 15 ses 40 42 makrong kasanayan sa pagbabasa
Wk 13 ses 35 37 makrong kasanayan sa pagsasalita
Wk 12 ses 32 34 makrong kasanayan sa pakikinig
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1

Recently uploaded (20)

PPTX
Introuction about WHO-FIC in ICD-10.pptx
PDF
Testing WebRTC applications at scale.pdf
PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
Β 
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PPTX
Digital Literacy And Online Safety on internet
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PDF
Paper PDF World Game (s) Great Redesign.pdf
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
Β 
PPTX
Funds Management Learning Material for Beg
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PPTX
innovation process that make everything different.pptx
DOCX
Unit-3 cyber security network security of internet system
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
Internet___Basics___Styled_ presentation
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
Introuction about WHO-FIC in ICD-10.pptx
Testing WebRTC applications at scale.pdf
Triggering QUIC, presented by Geoff Huston at IETF 123
Β 
Cloud-Scale Log Monitoring _ Datadog.pdf
Digital Literacy And Online Safety on internet
WebRTC in SignalWire - troubleshooting media negotiation
Job_Card_System_Styled_lorem_ipsum_.pptx
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Paper PDF World Game (s) Great Redesign.pdf
RPKI Status Update, presented by Makito Lay at IDNOG 10
Β 
Funds Management Learning Material for Beg
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
SASE Traffic Flow - ZTNA Connector-1.pdf
innovation process that make everything different.pptx
Unit-3 cyber security network security of internet system
PptxGenJS_Demo_Chart_20250317130215833.pptx
Internet___Basics___Styled_ presentation
522797556-Unit-2-Temperature-measurement-1-1.pptx
Tenda Login Guide: Access Your Router in 5 Easy Steps

Php forms

  • 1. PHP Forms  GET and POST Method  Form Validation  Form Required Fields
  • 2. *Property of STI K0032 GET and POST Method ο‚ A form data can be submitted using POST and GET method ο‚ Both are used for same purpose but stand apart for some specifications ο‚ GET and POST create an array which holds key/value pairs, where keys are the name of the form controls and values are the input data by the user
  • 3. *Property of STI K0032 GET and POST Method ο‚ Both GET and POST method are treated as $_GET and $_POST in PHP ο‚ These methods are superglobals, which means that they are always accessible, and they can be accessed using any function, class or file ο‚ The $_GET method is an associative array of variables passed to the current script via the URL parameters
  • 4. *Property of STI K0032 GET and POST Method ο‚ The $_POST method is an array of variables passed to the current script via the HTTP POST method ο‚ In this method the information is transferred in a hidden manner ο‚ A form that submits information is appended to the URL in the form of Query String which consists of name = value pairs in URL known as URL Encoding
  • 5. *Property of STI K0032 GET and POST Method ο‚ This string contains user values/data, which are joined using equal (=) signs, separated by ampersand (&), and spaces are removed and replaced with plus (+) sign Name1=value1&name2=value2&name3=value3
  • 6. *Property of STI K0032 Get Method ο‚  http:/.www.example.com/index.html? name=mel@email.com&contact=09176543210 ο‚  The code below is a client-side HTML form using method=β€œget” for user to fill the information
  • 7. *Property of STI K0032 Get Method ο‚  The code below is the server-side PHP script where, $_GET associative array is used to receive sent information from server end
  • 8. *Property of STI K0032 Post Method <form action="#" method="post"> .... </form> Below is a server-side PHP script where $_POST associative array is used to receive sent information at server end
  • 9. *Property of STI K0032 Form validation
  • 10. *Property of STI K0032 Form validation The form shown in Figure 6.1 consists of the following elements: ο‚  Name (required field - must contain letters and whitespaces) ο‚  E-mail (required field - must contain valid email address) ο‚  Website (optional field - if present, must contain valid website URL) ο‚  Comments (optional field - a multi-line text field) ο‚  Gender (required field - must select a radio button )
  • 11. *Property of STI K0032 Form Elements ο‚ The Name, E-mail, Website are input elements ο‚ Input elements, in particular, used text and submit values for its types attribute in order to create text fields and buttons ο‚ The HTML code:
  • 12. *Property of STI K0032 Form Elements ο‚ Radio button shows several options to the users from which the user may select one ο‚ HTML Code:
  • 13. *Property of STI K0032 Form Elements ο‚ The text area is typically a large text field with multiple rows ο‚ The textarea element has three attributes – name, rows, and cols attribute ο‚ HTML code:
  • 14. *Property of STI K0032 Form Elements ο‚  list element offers options from which the user might choose. A list can be created using the select element, within which is nested option elements for each option to appear ο‚  The select element has a name attribute giving the name for the browser to use when identifying the selection when the form is submitted ο‚  The option element has a value attribute for specifying what value to send when that option is selected, and it has a select attribute which allows the HTML to specify which option is initially selected. The code
  • 15. *Property of STI K0032 Form Elements ο‚  HTML Code:
  • 16. *Property of STI K0032 Form Element ο‚  The HTML code of the form element: ο‚  when the form is submitted, the form data is sent with method=”post” ο‚  So, the $_SERVER["PHP_SELF"] sends the submitted forms data to the page itself, instead of jumping to a different page
  • 17. *Property of STI K0032 Form Element ο‚  The $_SERVER["PHP_SELF"] is a super global variable that returns the filename of the currently executing script ο‚  Htmlspecialchars() function converts special characters to HTML entities ο‚  Cross-site scripting (XSS) is a type of computer security vulnerability typically found inWeb application
  • 18. *Property of STI K0032 Form Element ο‚  Example: test_form.php ο‚  if a user enters the normal URL in the address bar like "http://www.example.com/test_form.php", the above code will be translated to:
  • 19. *Property of STI K0032 Form Element ο‚  consider that if a user enters the following URL in the address bar: http://www.example.com/test_form.php/%22%3E %3Cscript%3Ealert('hacked')%3C/script%3E ο‚  will be translated to:
  • 20. *Property of STI K0032 Form Element be aware that any JavaScript code can be added inside the <script> tag A hacker can redirect the user to a file on another server, and that file can hold malicious code that can alter the global variables or submit the form to another address to save the user’s data
  • 21. *Property of STI K0032 how to avoid $_SERVER[β€œPHP_SELF”] exploit? ο‚ The $_SERVER[β€œPHP_SELF”] exploit can be avoided using the htmlspecialchars() function ο‚ if the user tries to exploit the PHP_SELF variable, it will result:
  • 22. *Property of STI K0032 Validate Form Data with PHP ο‚  The very first thing to do to validate form data with PHP is to pass all variables through PHP’s htmlspecialchars() function ο‚  For example: ο‚  With htmlspecialchars() function it would not be executed, because it would be saved as HTML escaped code like this:
  • 23. *Property of STI K0032 ο‚ test_input()
  • 24. *Property of STI K0032 Form Required Fields ο‚  In the previous slide, all input fields were optional, meaning no required fields to be filled in by the user ο‚  Here is a simple PHP script that checks the name for empty input and throws an error message if the input is empty:
  • 25. *Property of STI K0032 Form Required Fields To display the error message in the HTML form (this will be generated if the user tries to submit the form without filling in the required fields) use the code below: