Join David Powers for an in-depth discussion in this video Retrieving values from a URL's query string, part of PHP for Web Designers.
- View Offline
there are two methods of submitting a form, the post method as you've already seen transmits the input data in the backgroung with the HTTP headers. The get method on the other hand adds the input data as a series of name value pairs in a query string at the end of the URL, this page contains a simple search form. An input field with the name searchterm. And the submit button with the name search. So let's see how the query string is created in the browser.
So we need to go to the correct page, we're in the 05_02 folder, and the name of the page is get.php. There's our form, let's search for some flowers, let's try for some daisies and click search and the page has reloaded and the URL now has a query string. It's that question mark followed by search term which is the name of the search field. And the value that was inserted in it equals daisies ampersand search equals search.
That's the name of the submit button and also the value that's displayed on the submit button. So, if I search for a different type of flower, let's say we search for some orchids. And submit search. Search term changes to orchids. So how do you get the values out of a query string? Well, let's go back to our editing program. PHP automatically creates an associative array called get which uses the name attribute as the key for each array element of a form that has been submitted using the get method.
So let's display our search term in the page. And after the form, we need to add a PHP code block first. We need to make sure that get search term has been set, because if we try to display it, if it hasn't been set, we'll get an error message. So, we need an if statement, and the condition will be, using the isset function, and we are looking for GET which is $_GET, all in capitals, it's case sensitive.
And then the array element that we're looking for is search term. And then we need an opening curly brace and then I'm going to close my PHP block there and I'm going to add a paragraph. And we'll say, you searched for. And then another opening PHP tag and we'll echo that value. So it's echoing the GET array and the element we're looking for is search term.
Then we'll have a period and a closing paragraph tag and then of course we need to close that if statement, we need another PHP block just for the closing curly brace. Save that page. Go back to the browser. And this time, I'm going to put in, what shall we look for this time? Daffodils, shall we? And click Search. You searched for daffodils. And if I leave the field empty and click search, you searched for blank.
That's an important thing to notice. That the variable is set, even if the search field is empty. So the GET array is another of PHP's super global arrays that are automatically formed whenever a page is requested. It stores the name value pairs appended to the end of the URL in a query string. The name is used as the array key. In this example, we use the search form submitted by the GET method but as you'll see in the next chapter, the GET array can also be used to extract the values from any query string.
Note: This course was recorded with Adobe Dreamweaver, but you can use the code editor of your choice to follow along.
- How to use PHP in a webpage
- Storing and displaying text and numbers in variables
- Using functions
- Adding comments to your PHP scripts
- Moving common page elements to include files
- Displaying different content on a page
- Working with multiple values in arrays and loops
- Getting form input sent by the POST method
- Using PHP sessions to preserve data
- Storing data in session variables
- Connecting to and querying a database
- Handling errors