Lecture 21
Passing Variables Between
Pages
Mr. Mubashir Ali
Lecturer (Dept. of Computer Science)
dr.mubashirali1@[Link]
1
Summary of the previous lecture
• Operators in PHP
• Conditional Statements in PHP
• Looping Statements
• Arrays in PHP
Mubashir Ali - Lecturer (Department of
2
Computer Science).
Outline
• Super Global variables
• Passing form data
• Passing data with sessions
Mubashir Ali - Lecturer (Department of
3
Computer Science).
1. Passing form data
• Forms provide a mean of submitting
information from the client to the server
• We can create HTML forms using <form>
tag
• Method and action are the most
common attributes of <form>
Mubashir Ali - Lecturer (Department of
4
Computer Science).
1. Passing form data…
• action - gives the URL of the application
that is to receive and process the forms
data
• method - sets the HTTP method that the
browser uses to send the form's data to
the server for processing
– most common methods are POST or GET
Mubashir Ali - Lecturer (Department of
5
Computer Science).
1. Passing form data…
• Get method : all form data is encoded
into the URL, appended the action URL
as query string parameters
Asad
asd@[Link]
submit
Action page name Input field Value entered
name by user
Mubashir Ali - Lecturer (Department of
6
Computer Science).
1. Passing form data…
• Post method: form data appears within
the message body of the HTTP request
Mubashir Ali - Lecturer (Department of
7
Computer Science).
1.1 Super Global Variables
• PHP automatically makes few variables
available in your program
• These are array variables and can be
accessed by name
• These variables are called super-global
variables because they can be accessed
without regard to scope
Mubashir Ali - Lecturer (Department of
8
Computer Science).
1.1 Super Global Variables…
• $_GET: contains all the query string
variables that were attached to the URL
• $_POST: contains all the submitted
form variables and their data
Mubashir Ali - Lecturer (Department of
9
Computer Science).
1.1 Super Global Variables…
<body> Asad
<form method=“get”
action=“[Link]”> asd@[Link]
<input type=“text” name=“name”> submit
<input type=“text” name=“email”>
<input type=“submit”>
</form>
</body>
name email
$_GET
Asad asd@[Link]
Mubashir Ali - Lecturer (Department of
10
Computer Science).
1.1 Super Global Variables…
<body> Asad
<form method=“post”>
<input type=“text” name=“name”> asd@[Link]
<input type=“text” name=“email”> submit
<input type=“submit”>
</form>
</body>
$_POST
Asad asd@[Link]
name email
Mubashir Ali - Lecturer (Department of
11
Computer Science).
1.2 Accessing form data on action page
name email
$_GET
Asad asd@[Link]
[Link]
Asad
<?php
asd@[Link] $name = $_GET[‘name’];
submit $email = $_GET[‘email’];
?>
Mubashir Ali - Lecturer (Department of
12
Computer Science).
1.2 Accessing form data on action page…
name email
$_POST
Asad asd@[Link]
[Link]
Asad
<?php
asd@[Link] $name = $_POST[‘name’];
submit $email = $_POST[‘email’];
?>
Mubashir Ali - Lecturer (Department of
13
Computer Science).
2. Passing text field data
Post Method
Text field Text field
name
Mubashir Ali - Lecturer (Department of
14
Computer Science).
2. Passing text field data…
Data is received
Display a message
Mubashir Ali - Lecturer (Department of
15
Computer Science).
2. Passing text field data…
We are at form page
We are on
action page
Mubashir Ali - Lecturer (Department of
16
Computer Science).
2. Passing hidden field data…
Hidden Field name Hidden value
field
Accessing hidden value
Mubashir Ali - Lecturer (Department of
17
Computer Science).
2. Passing hidden field data…
Mubashir Ali - Lecturer (Department of
18
Computer Science).
2.3 Getting value from checkbox
name value
label
Getting
value of ‘C’
Getting value
of ‘VB’
Mubashir Ali - Lecturer (Department of
19
Computer Science).
2.3 Getting value from checkbox…
Mubashir Ali - Lecturer (Department of
20
Computer Science).
2.3 Getting value from checkbox…
Checking for value of C
Mubashir Ali - Lecturer (Department of
21
Computer Science).
2.4 Getting value from radio button
Same name Value is set
Value is not set
Getting value of
radio
Mubashir Ali - Lecturer (Department of
22
Computer Science).
2.4 Getting value from radio button…
Mubashir Ali - Lecturer (Department of
23
Computer Science).
2.5 Getting value from select list
Name of the list
Option and value
Mubashir Ali - Lecturer (Department of
24
Computer Science).
2.5 Getting value from select list…
Mubashir Ali - Lecturer (Department of
25
Computer Science).
3. Passing variables using sessions
1. A session is basically a temporary set of
variables that exists only until the browser
has shut down
2. $_SESSION: represents data available to a
PHP script that has previously been stored in
a session
Mubashir Ali - Lecturer (Department of
26
Computer Science).
3. Passing variables using sessions…
name email
$_SESSION
Asad asd@[Link]
First page 2nd page nth page
<?php <?php <?php
$_SESSION[‘name ‘] echo echo
=‘Asad’; $_SESSION[‘name ‘]; $_SESSION[‘name ‘];
?> $_SESSION[‘email ‘] ……. echo
=‘asd@[Link]’; $_SESSION[‘email ‘];
?> ?>
Mubashir Ali - Lecturer (Department of
27
Computer Science).
3. Passing variables using sessions
• session_start()- is used to start a session
• $_SESSION[‘variable_name’]- is used to store
data in session variable
• session_destroy()- is used to destroy a session
• unset($_SESSION[‘variable_name’])- is used to
unset a specific variable
Mubashir Ali - Lecturer (Department of
28
Computer Science).
3. Passing variables using sessions…
Session starts
Session variable is
created
Link to the next
page
Mubashir Ali - Lecturer (Department of
29
Computer Science).
3. Passing variables using sessions…
Session starts
Session variable is accessed
link
Mubashir Ali - Lecturer (Department of
30
Computer Science).
3. Passing variables using sessions…
Session
variable’s value
Mubashir Ali - Lecturer (Department of
31
Computer Science).
3. Passing variables using sessions…
Session is
destroyed
Session is
accessed
Mubashir Ali - Lecturer (Department of
32
Computer Science).
3. Passing variables using sessions…
Mubashir Ali - Lecturer (Department of
33
Computer Science).
Summary
• Super global variables
• Passing data with forms
• Using session variables
Mubashir Ali - Lecturer (Department of
34
Computer Science).
References
• Chapter 2, “Beginning PHP6,Apache,Mysql
web development” by Matt Doyle, Wrox
publishers, 2009, ISBN: 0470413964
• Chapter 13, “Beginning PHP and MySQL” by
W. Jason Gilmore, Apress publisher, 4th
edition; 2010, ISBN-13 (electronic): 978-1-
4302-3115-8.
Mubashir Ali - Lecturer (Department of
35
Computer Science).