Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we have our PHP fundamentals, we're ready to start building webpages with PHP. Think back for a moment to the diagram that we saw, with the request response cycle, that shows requests starting at the browser, going to the web server, the web server processes our PHP and returns an HTML page back to the browser. That cycle, that request response cycle begins with a browser request. Nothing happens on the web server unless the user sends in some information to us. And there are only three ways that we can get data from users on the web. There's URL's and links.
There's forms, web forms that you fill out with information and click submit, and then there's cookies. Those are browser cookies that are stored on the browser. And each one of these corresponds to an HTTP method. The first is Get, the second is Post and the last is Cookie. So, URLs and Links are Get requests, a form is a Post request. And Cookie, well, it's not really a cookie request, but it's the way that we access the cookie information that piggybacks on each request. So, these are the three main things that we're going to be looking at. Now, every web language has a different way to interact with these three types of user data, and over the next three chapters, we'll look at how it's done in PHP.
We'll start by looking at URLs and links. So to begin looking at links, let's first go to our basic.html page. And let's do Save As on that. And we're going to save the first one as firstpage.php. And I'll save that. And now, let's go ahead and, before we do anything else let's do another Save As. And we're going to change this one to secondpage.php. That will make it really obvious that we have these two pages, and I'm also going to go and open them both up so that they're both visible. So here's first page, and here's second page.
Now I can switch back and forth, and I can start to make links between them. That's what I want to do here, that's how we're going to see this. So let's go ahead and give this one a title of First Page and save it. We'll this one a title of Second Page. Now, we'll be able to see that in the browser and know which one is which. So let's go to first page, and let's make a link on First Page. A link in PHP is just an HTML link, that's all it is. So open our tag and then a href equals, and then inside quotes we're going to put where the page is going to go to, then we need some text for it, call it second page, and then close the A tag.
So where are we going to link it to? Second_page.php. That's going to link it to the second page. Now, that's not PHP, that's just basic HTML. We don't even have PHP tags in that. Let's go and bring that up in a browser. Localhost, for me it's going to be kevinstovand/sandbox and then firstpage.php. There we are, there's my link. Nice and simple. I click on it, and there I am, now I'm on the second page. See how that worked? I can click the back button, go back to the first page, and link back to the other one. So that's it. That's all there is to making links, in PHP is to make HTML links. Now, the great thing about PHP though is that we can start to improve these links. We can make them more dynamic.
So, for example, instead of having second page, we can cut that and let's go up here. And let's make a PHP tag, and let's call this link name equals and then in quotes, second page. Now, we could use our PHP to provide the text. PHP Echo, and then dollar sign, link name. Now, make sure that you use Echo, very important here. We're outputting code into the HTML, so we need that echo. Super important, don't forget it. This won't get the job done for you.
So, now let's just bring it up and try it again real quick. We'll go back to our first page and let's just reload the page, and you see that we still see the second page. So now we've got some dynamic text, this is something that could potentially be pulled from a database, for example. Or maybe we have an if else statement that decides if one condition is true, give it the length this name, if it's something else, give it a different name. We can also have PHP that decides which link to display. So it's either log in if you're not logged in, or it's log out if you are already logged in. One of the ways that we send a lot of dynamic data, is by sending something in the query string.
The query string is what comes after the page name. So for example, id equals 1, that would be a static link. Let's just save that. Come back over here and reload the page. And now when I go to second page, you can see it says PHP id equals 1. That's the kind of thing that PHP is really good for. Let's imagine that we had a loop for a second. We could put a loop around this tag, and then maybe we were outputting a list of categories on a website or subjects. We could go through and use a loop to iterate through each of the subjects. So we can have a subject id equals 1, this link, subject id 2 equals this length.
Let's go ahead and just make PHP equals, and I'll just make 2 for now, just so that's something a little different. And then here, PHP, remember we need that echo, and then id. There we go. Rmemeber to do those echoes. Now we come back here to first page, reload the page, and the link is going to go id two. You can see that the very bottom of the page, the browser gives me a hint for that, where I can click on it and see now id is equal to two. So try that. If you want to try putting this inside the loop and looping from the numbers one through ten.
And then you can see that every time id will increment, and you would then have links that go to different ids. Now, once we send it to the other page though, the second page needs to be able to pick up on that. The second page, it's very important that it says, alright, I have an id 2 that requires some different processing than if I had id number four. Otherwise it's not a dynamic page, so how do we get that get value to work with. We'll look at that in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97164 Viewers
61 Video lessons · 84457 Viewers
71 Video lessons · 68665 Viewers
56 Video lessons · 101148 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.
Your file was successfully uploaded.