Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie, we'll learn how to send a value from one page to the next by using the URL query parameters. The query parameters are the part of the URL that comes after the question mark. This is typically were you send additional parameters that the page needs. The format is always the name of the parameter, an equals sign and then the value of the parameter. Often these parameters modify the behavior of the code used to generate the returned HTML. For example, somepage.php?page=2 would process the same PHP code but the content return would probably be modified. It would do something like return a second page of search results. And you can send more than one query parameter in the URL by using an ampersand between each one of these parameters.
You can surf around the web and see lots of examples on sites like Google and Amazon where you can see these query parameters being used. It happens all over the web. Now in PHP whenever a page request is made, PHP is automatically going to take all of those query parameters that were sent with the URL and put them into an associative array where we can access them. That array is what is called a Super global variable. We've talked about global variables before and we saw how we needed to call global on them to bring them into the local scope. Well super global's are always available in all scopes and PHP set's them for us before the page even starts processing. There are about nine super globals altogether.
And we'll be looking at several of them later in this training. But right now we're only concerned with the super global that relates to the variables passed to the page via those URL parameters. And that one is called GET. And that makes sense, because GET is the HTTP method that relates to URLs and links. And the way that we access it is with dollar sign, underscore and then all capitals GET. That's the name of the super global where the query parameters are put by PHP. Notice the underscore that's in the begining. That's how all super globals are going to be named.
Incidentially this is why in the movie about variables I suggested that you should not name your variables with underscores at the start. It helps to make these super globals really stand out. And because it's in all capitals don't get it confused with being a constant. It is still a variable, it has that dollar sign at the beginning. It's a super global variable. Alright, so now that we know where these are stored, let's see how we access them. In the last movie, we created first page.php and we also created second page.php. And we created a link on the first page and that link sends a get request to the second page. And all of the query parameters are sent with that link. In this case we're sending id equals 2.
We're doing that right here, id equals and then the value of id, which is 2. And all of those query parameters that are sent with that link are going to be stored in the super global get. So the get super global is going to be an associative array. So let's use print r so that we can take a look at the contents of that array. So let's just go to second page, here we are. And in second page I'm going to use my pretags first because that's going to make it look nice with print r and then we'll use php print r.
And we'll use our super global GET and let's just take a look at what that value looks like. We're just inspecting it. Let's go back here. Now typing a URL in the browser bar is 100% the same as clicking on a link. We don't have to go back to first page we can just re-load this page. Or we can type the URL in directly. Both of those make GET request to our server. So let's re-load it and there it is array id equal to 2. And we know how to access values that are in the array. Before we do that, though, I just want to show you, let's take away this parameter so that there is nothing after it and let's just hit Return to load that URL.
And you see that comes back empty. You can put anything you want up here in the string to try it, 34&name=kevin, and whatever's up here just gets dropped into our associative array. So if our link sends these parameters, if that's what our link looks like then that's what will be sent to the second page. Okay, so we know how to access the values that are stored in associative arrays. I'm just going to make my little window here a little smaller so I can now see. Here we go, second page. What we want to do now is instead of doing print r, let's just comment that out. And instead, let's set id equal to $ Get and then the key that goes with it, id.
That's it. So that will now take that value that was in that associative array and assign it to the variable id. Now we can work with it. Let's just echo it for now, echo id back, let's go back and let's reload this page, I'm going to go all the way back to first page, there we are, so now I'm on first page, let's reload that... Click second page and there it is. It output the 2 for us. Now I've still got my pre tags around all of this which is why it put the 2 way out here in the middle. You could also just move this down here and that's outside the pre tag. So let's do a new block of php.
All right, so that's just a little cleaner. We'll go back and reload the page and there it is, outputting our value of 2. So let's go back to our first page. And let's say that first page sent an id of 5. Save that. We'll go back to first page, reload that page. Must reload it so that we get that new value in here. because otherwise the HTML that's here on the page is the old HTML. So we reload the page. Now I click it and it comes up with 5. So we've already made our website a lot more dynamic by allowing us to pass values between pages in these URL query parameters.
This raises an important issue though. Can a user send any value in that URL? For example, what if their value had an ampersand in it? Would it still be read correctly or would it interfere with the processing of that URL? Well there are some characters that we have to watch out for. And in the next movie we're going to look at how to encode those values to make sure that they work properly.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105445 Viewers
56 Video lessons · 117130 Viewers
71 Video lessons · 86354 Viewers
131 Video lessons · 41269 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.