PHP & MySQL Tutorial - 40: Passing Values Through Query String

Sharing buttons:

hello guys welcome to my channel this is

the 40th tutorial in this course and in

this tutorial we are going to begin our

discussion on how we can pass values

from one script in PHP to another script

and this is actually the sense of web

scripting you know this is how we make

dynamic websites and this is something

that you would be required to do quite a

lot as a web developer and we see this

you know everywhere like you know even

when you're making a Google search you

see you know your keywords whatever

you've typed in in the search box being

transferred to the server within the

query string and you know if you sign up

for a website or you know even if you

try to log in you know your values

whatever values you provide in form

fields are actually sent to the server

then form validation happens and then

you get you know something back as a

response so we're going to check out

three methods of passing values from one

file to another and the first way that

we're going to check out is you know how

we can pass in values within the query

string and the second method is going to

be how we can use form third one is

going to be how we can use cookies right

and we're going to like you know have

about five or six tutorials on these

three topics alone and this is really

important as I said so let's not waste

time I've got our two files opened up in

my text editor the first one is called

source dot PHP and the second one is

destination dot PHP and you know these

are really short script files I try to

keep you know them as simple as possible

and what we're going to do is or what we

are trying to do is we're trying to pass

in three values you know information

about a person called John so John is

the name of the guy and his age is 22

and his email address is John at

gmail.com so we want to draw up you know

paths in these three values from the

source file the destination file or you

know some place in the server and we are

going to collect these values in a

superglobal called get so you know this

you can think of like an array

associative array and you know as soon

as you pass in values within the query

string and this is something we're going

to check out on just a minute

you know these values get collected in

this array called dollar underscore get

or the get super global and you know

it's indices are actually the names of

you know the variables that you've

passed to it right so if suppose you've

passed a name is an email these are

going to be the keys for this array and

using these keys you can get in the

values and you can you know do whatever

you want to do with those values so

let's first check out what we've done in

the source file so I have created these

three variables you know name gets the

value John age gets the value 22 email

gets the value John at gmail.com and

then I have created a string variable

called STR and it has the following text

so I first have the text name equals and

this has to match you know whatever a

word I have here whatever text I have

here has to match you know whatever key

I use for my get array so over here I'm

using name so that's why this exactly

has to be named and then I have the you

know equal to symbol or the sign

interpreter and then I have you know

passed in the value that's contained in

the variable name right so this doesn't

have to be necessarily the same as this

but you know whatever a text I'm using

here as I said has to match the key that

I've used in the GATT superglobal and

similarly I have you know typed in the

text age and then I have passed to it

the value that's contained in the

variable age and then email and then

I've used the equal to symbol and then

the text that's there in the variable

email one thing that you must have

noticed is that between these you know

text fields

I actually have an ampersand operator

and this is how you separate values so

if you're sending multiple values then

you have to separate them using the

ampersand symbol and then uh you know I

have the closing PHP tag in the file on

line number 11 or line number 12 I have

an anchor tag so we are actually going

to display a link on the screen and only

when the user clicks on the link these

values are going to be transferred and

where are they going to be transferred

they're going to be transferred to the


nation dot php' so the xref a tribute

gets the value destination dot php' and

then i have a question mark and i have

again you know started another PHP site

so i have the opening PHP tag here and

then I've used the echo statement to

output the contents of the string

variable st st AR so this is going to be

my query string and this will be

outputted here immediately after the

question mark and then I close the PHP

tag and I have the you know double

quotation mark for closing this string

and then I have the text destination so

this is all that's going to be seen on

the output screen when we execute the

script file and I've closed the anchor

tag right so let me show you guys the

output of this file before we proceed

further so instead of default PHP I am

going to look for source dot PHP when I

press the Enter key this is all that I

see right so it's a link because when I

hover over it we can see that you know

the pointer is changing and if I click

on it the values will be passed and let

me actually show you guys the output of

the destination page as well so here we

go we see name is John a just trying to

email as John at gmail.com so these

values have been passed and you can be

sure that I haven't uh redefined these

variables here you know I haven't typed

in these values these values have

actually been transferred so you know

when I use the key name with the get

super global you know the value John was

fetched and it was put in another

variable called name in this file and

this variable name is you know even

though it has the same name as this

variable name but then since these two

variables are in different files you

know this one's actually are nothing to

do with the variable in the source file

and similarly for age and email we've

obtained the values by using age as the

key here and email as the key here and

then on line number ten I have an echo

statement to see the value in the

variable name on line number eleven I

have an echo statement to see the value

in the variable age and on line number

twelve I have an echo statement see the

value in the variable email right so

that's all that's happening in this

script file and this is a very popular

method of passing and values but then

it's not a secure method because you

know as you can see here in the browser

that in the URL bar here you can

actually see the values that have been

passed right so name is John ages 22 and

email as John and gmail.com so you know

if you were passing keywords that a

person has searched for you know like

Google does that's fine you know there's

nothing wrong with that but then if

you're you know obtaining passwords from

people or information that people would

not want people who are sitting you know

around them to see then you would

probably not want to use this method you

know you would want to encrypt the

values in some way or you would want to

use a you know forms if you are getting

some something you know that could be

confidential so anyway that's it for

this tutorial and I hope you guys had

fun watching this one and you may

subscribe to my channel if you haven't

already I'm going to see you in the next

video in which we continue our

discussion on this topic and thank you

so much and I'll see you soon till then

take care