You are on page 1of 34

Passing variables between pages

1
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>

2
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

3
1. Passing form data…
• Get method : all form data is encoded
into the URL, appended the action URL
as query string parameters
Asad

asd@gmail.com
submit

4
1. Passing form data…
• Post method: form data appears within
the message body of the HTTP request

5
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

6
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

7
1.1 Super Global Variables…
<body> Asad
<form method=“get”
action=“action.php”> asd@gmail.com
<input type=“text” name=“name”> submit
<input type=“text” name=“email”>
<input type=“submit”>
</form>
</body>

name email
$_GET
Asad asd@gmail.com
8
1.1 Super Global Variables…
<body> Asad
<form method=“post”>
<input type=“text” name=“name”> asd@gmail.com
<input type=“text” name=“email”> submit
<input type=“submit”>
</form>
</body>

$_POST
Asad asd@gmail.com
name email

9
1.2 Accessing form data on action page
name email
$_GET
Asad asd@gmail.com

Action.php
Asad
<?php
asd@gmail.com $name = $_GET[‘name’];
submit $email = $_GET[‘email’];
?>

10
1.2 Accessing form data on action page…
name email
$_POST
Asad asd@gmail.com

Ation.php
Asad
<?php
asd@gmail.com $name = $_POST[‘name’];
submit $email = $_POST[‘email’];
?>

11
2. Passing text field data

Post Method

Text field Text field


name

12
2. Passing text field data…

Data is received

Display a message

13
2. Passing text field data…
We are at form page

We are on
action page

14
2. Passing hidden field data…

Hidden Field name Hidden value


field

Accessing hidden value

15
2. Passing hidden field data…

16
2.3 Getting value from checkbox
name value
label

Value = “on” /> VB

Getting
value of ‘C’

Getting value
of ‘VB’

17
2.3 Getting value from checkbox…

18
2.3 Getting value from checkbox…

Checking for value of C

19
2.4 Getting value from radio button
Same name Value is set

Value is not set

Getting value of
radio

20
2.4 Getting value from radio button…

21
2.5 Getting value from select list
Name of the list

Option and value

22
2.5 Getting value from select list…

23
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 temporarily stores the information on the
server. If we want to store the information
permanently than we need to use database
(mysql-phpmyadmin)
3. $_SESSION: represents data available to a PHP
script that has previously been stored in a session

24
3. Passing variables using sessions
1. A session is usually used when we create a
user login form.
2. When user login, a session is created and after
logout session has been deleted.

25
3. Passing variables using sessions…
name email
$_SESSION
Asad asd@gmail.com

First page 2nd page nth page


<?php <?php <?php
$_SESSION[‘name ‘] echo echo
=‘Asad’; $_SESSION[‘name ‘]; $_SESSION[‘name ‘];
?> $_SESSION[‘email ‘] ……. echo
=‘asd@gmail.com’; $_SESSION[‘email ‘];
?> ?>

26
Steps to set and get SESSION values

27
3. Passing variables using sessions
• session_start()- is used to start a session
• $_SESSION[‘variable_name’]- is used to store
data in session variable
• unset($_SESSION[‘variable_name’])- is used to
remove all session variables that are created
when the session is start.
• session_destroy()- is used to destroy a session

28
3. Passing variables using sessions…
Session starts

Session variable is
created

Link to the next


page

29
3. Passing variables using sessions…
Session starts

Session variable is accessed

link

30
3. Passing variables using sessions…

Session
variable’s value

31
3. Passing variables using sessions…
Session is
destroyed

Session is
accessed

32
Example

33
Thank you

34

You might also like