fetch

Passing variables using the querystring in PHP



Sharing buttons:

okay so we know how to create variables

in our PHP pages we know how to show

them on the screen but up to now we've

just been working with single PHP pages

and before we move on to the next

section I just want to point out one way

that we can pass information between our

pages using the web address query string

now I've created two simple pages here

to demonstrate this

so in page one dot PHP we've got a

variable called color with a bit of text

in there that says blue and then we're

printing out using string concatenation

the variable color or rather the

variable called color contains the word

and then the contents of that variable

and then we've got a hyperlink to page

two and that's where it looks like in

the web browser and then when the user

clicks on the link they go to page two

which is this bit of called pretty much

the same chord the only difference is we

haven't declared the variable again so

what we are do you want to be able to do

is show the contents of that variable

here but you'll notice on page two it's

empty

so page one the variable is there when

we got a page two the variable is not

displayed so you might be thinking well

what's going on there it's part of my

same application why is the variable

which I've declared on page one dot PHP

not being displayed on page two dot PHP

and this has something to do with the

way the web works every time we request

a new web page effectively the web

server just processes that page it

doesn't really do anything about the

previous page so we have to as

application developers construct a way

of sharing this information so let's go

back to page two dot PHP and let's see

if we can work out using some of the

tools we've already explored we might

want to use the variable dump for

example and let's have a look at that

variable we save it and refresh the page

it's coming back with null the variable

doesn't exist okay so one way that we

can share this information like I said

at the beginning of the video is to pass

the information along on the address bar

so if we go down to our hyperlink at the

bottom what we can do is after the page

put a question mark and pass some extra

information over so we could say color

equals and then the contents of that

variable so if I copy that and paste it

onto there and serve it what you'll see

now is if I go back to page one and

refresh the page I'm going to roll over

the hyperlink and if you look at the

bottom of the bottom of the web browser

that's the status bar you can see it

says color equals dollar color and we

want that to actually content blue not

actually print out dollar color so any

ideas why you think that's not working

well the reason that's not working is

because this is a variable but we're

just in HTML mode we're not in PHP mode

because we started the PHP tag at the

top and then stop the PHP so if we want

to actually go back into PHP mode fee

for this bit we would have to do

something like this turn on PHP print

the variable and then turn off PHP okay

so we've created a hitch to email

hyperlink added the variable in clause

the hyperlink so if I save that go back

to the browser and press refresh now if

I roll over the link and look at the

status bar it says color equals blue

okay so I'm still going to page to dot

PHP when I click the link but now I'm

passing over an extra piece of

information on what we call the query

string so I'm going to click the link

and if you look at the address bar we

says color equals blue now at the moment

we still are showing the old bit of

court so let's go and fix that create a

variable here and call it color and then

into that variable I want to go on

access what's a new dress bar so go and

get me this little parameter here called

color now the way we do that again

there's a number of different ways of

doing it but a simple way to demonstrate

in practice is to use a superglobal

called get will put up a square bracket

and then pass in the name that we're

after which in this case is cooler and

in fact I've kept all of my variable

names to say member line so create a

variable assignment look in the address

bar find me the name of the parameter

called color and assign it so now if I

save that go back to my browser and

press refresh there we go back to one

back to page two now I could actually

modify this directly so if I just type

in green you'll see how it's changing

now I've done a very simple example

there I haven't done any sort of

sanitization having cleaned any of the

text up in any way that is actually

potentially open to exploitation which

you'll see later in and the course so we

would need to do some additional work

before we would put this into a

production environment all right but it

just demonstrates basic parameter

passing using the query string