Professional Documents
Culture Documents
Website BCA
Development using
PHP & MySQL
PHP Forms
2
What is Form?
●
Forms are used to get input from the
user and submit it to the web server for
processing.
●
The diagram below illustrates the form
handling process.
3
When & Why From?
●
Forms come in handy when developing flexible and
dynamic applications that accept user input.
●
Forms can be used to edit already existing data
from the database
●
Following things are required to create a
form:
– Opening and closing form tags <form>…</form>
– Form submission type POST or GET
– Submission URL that will process the submitted data
– Input fields such as input boxes, text areas,
buttons,checkboxes etc.
4
Submitting data to Server
●
The action attribute of the form specifies the
submission URL that processes the data.
●
The method attribute specifies the
submission type.
●
Following are the Methods for submitting
data to server:
– GET
– POST
– REQUEST
5
Submitting data to Server
●
There are two ways the browser client can send information to the
web server.
– The GET Method
– The POST Method
●
Before the browser sends the information, it encodes it using a
scheme called URL encoding.In this scheme, name/value pairs are
joined with equal signs and different pairs are separated by the
ampersand.
●
name1=value1&name2=value2&name3=value3
●
Spaces are removed and replaced with the + character and any
other nonalphanumeric characters are replaced with a hexadecimal
values.
●
After the information is encoded it is sent to the server.
6
PHP Superglobal Variables
Several predefined variables in PHP are "superglobals", which
means that they are always accessible, regardless of scope - and
you can access them from any function, class or file without having
to do anything special.
●
$GLOBALS
●
$_SERVER
●
$_REQUEST
●
$_POST
●
$_GET
●
$_FILES
●
$_COOKIE
●
$_SESSION
7
PHP $GLOBALS
●
$GLOBALS is a PHP super global variable
which is used to access global variables
from anywhere in the PHP script (also
from within functions or methods).
●
PHP stores all global variables in an
array called $GLOBALS[index].
●
The index holds the name of the
variable.
8
PHP $SERVER
●
$_SERVER is a PHP super global variable
which holds information about headers,
paths, and script locations.
9
PHP $SERVER
$_SERVER['PHP_SELF'] Returns the filename of the
currently executing script
$_SERVER['SERVER_ADDR'] Returns the IP address of the
host server
$_SERVER['SERVER_NAME'] Returns the name of the host
server
$_SERVER['SCRIPT_FILENAME'] Returns the absolute pathname
of the currently executing script
$_SERVER['SERVER_PORT'] Returns the port on the server
machine being used by the web
server for communication (such
as 80)
$_SERVER['SCRIPT_NAME'] Returns the path of the current
script
10
PHP _POST Method
●
The POST method transfers
information via HTTP headers.
●
The information is encoded as
described in case of GET method and
put into a header called QUERY_STRING.
●
The array variable can be accessed from
any script in the program; it has a global
scope.
11
_POST Syntax
●
Syntax
<?php
$_POST['variable_name'];
?>
12
●
The POST method does not have any restriction on
data size to be sent.
●
The POST method can be used to send ASCII as well
as binary data.
●
The data sent by POST method goes through HTTP
header so security depend s on HTTP protocol.
●
By using Secure HTTP (https you can make sure that
your information is secure.
●
The PHP provides $_POST associative array to
access all the sent information using GET method.
13
14
_GET Method
●
The GET method sends the encoded
user information appended to the
page request.
●
The page and the encoded information
are separated by the ? Character.
●
The array variable can be accessed from
any script in the program; it has a global
scope.
15
Syntax
●
Syntax
<?php
$_GET['variable_name'];
?>
16
●
The GET method produces a long string that appears in
your server logs, in the browser's Location: box.
●
The GET method is restricted to send upto 1024 characters
only.
●
Never use GET method if you have password or other
sensitive information to be sent to the server.
●
GET can't be used to send binary data, like images or word
documents, to the server.
●
The data sent by GET method can be accessed using
QUERY_STRING environment variable.
●
The PHP provides $_GET associative array to access all
the sent information using GET method.
17
18
_GET vs _POST
19
REQUEST MEthod
●
The PHP $_REQUEST variable contains the
contents of both $_GET, $_POST, and
$_COOKIE.
●
The PHP $_REQUEST variable can be used to
get the result from form data sent with both
the GET and POST methods.
●
Here $_PHP_SELF variable contains the name
of self script in which it is being called.
●
$_REQUEST — HTTP Request variables.
20
Form Input
Attribute Values
Value Description
date Defines a date control (year, month and day (no time))
datetime- Defines a date and time control (year, month, day, hour,
local minute, second, and fraction of a second (no time zone)
Description
Value
range Defines a control for entering a number whose exact value is not
important (like a slider control). Default range is from 0 to 100
reset Defines a reset button (resets all form values to default values)
22
isset()
●
The isset () function is used to check
whether a variable is set or not.
●
The isset() function return false if
testing variable contains a NULL value.
23