LESSON 3
Form
Handling
Prepared By: Larra Mae Balisi 1
OBJECTIVES
Identify the two HTTP Differentiate the Create a program
Methods HTTP Methods that gets user
input using GET
and POST method
Prepared by: Larra Mae Balisi, CICS Faculty 2
Form Handling
<form action=“[Link]” method=“post”></form>
action method
WHERE HOW
Note: If action attribute is empty, the form data will be submitted to the
same URL it received it from.
Prepared By: Larra Mae Balisi 3
[Link] [Link]
<form action=“[Link]” method=“ ”>
John Doe John Doe
john@[Link] john@[Link]
SUBMIT
</form>
Prepared By: Larra Mae Balisi 4
HTTP (Hypertext
Transfer Protocol)
designed to enable communications
between clients and servers.
Prepared By: Larra Mae Balisi 5
Two HTTP Request Methods: GET and POST
POST - Submits
GET - Requests
data to be
data from a
processed to a
specified resource
specified resource
Prepared By: Larra Mae Balisi 6
The GET Method
• Note that the query string (name/value pairs) is
sent in the URL of a GET request:
/test/demo_form.php?name1=value1&name2=val
ue2
Prepared By: Larra Mae Balisi 7
The POST Method
• Note that the query string (name/value pairs) is
sent in the HTTP message body of a POST
request:
POST /test/demo_form.php HTTP/1.1
Host: [Link]
name1=value1&name2=value2
Prepared By: Larra Mae Balisi 8
GET POST
BACK button/Reload Harmless Data will be re-submitted
(the browser should alert
the user that the data are
about to be re-submitted)
Bookmarked Can be bookmarked Cannot be bookmarked
Cached Can be cached Not cached
Encoding type application/x-www-form- application/x-www-form-
urlencoded urlencoded or
multipart/form-data. Use
multipart encoding for
binary data
History Parameters remain in Parameters are not saved in
browser history browser history
Prepared By: Larra Mae Balisi 9
GET POST
Restrictions on data length Yes, when sending data, the GET No restrictions
method adds the data to the
URL; and the length of a URL is
limited (maximum URL length is
2048 characters)
Restrictions on data type Only ASCII characters allowed No restrictions. Binary data is also
allowed
Security GET is less secure compared to POST is a little safer than GET
POST because data sent is part of because the parameters are not
the URL stored in browser history or in
web server logs
Never use GET when sending
passwords or other sensitive
information!
Visibility Data is visible to everyone in the Data is not displayed in the URL
URL
Prepared By: Larra Mae Balisi 10
Exercise
• phpinfo();
• simple PHP browser detection script
<?php echo "Your User Agent is :" . $_SERVER
['HTTP_USER_AGENT’];
?>
• date(“g:i:s a”)
Prepared By: Larra Mae Balisi 11
Get user input
Prepared By: Larra Mae Balisi 12
isset($variable)
• true if variable has value
• false if null/no value
• used to check if form is submitted
if(isset($_POST[‘btn_submit’])){
//if clicked
}
Prepared By: Larra Mae Balisi 13
Exercise
Sample Output:
Name: John Doe
Age: 23
Address: Tuguegarao City
Prepared By: Larra Mae Balisi 14