From the course: Learning PHP

GET vs. POST - PHP Tutorial

From the course: Learning PHP

Start my 1-month free trial

GET vs. POST

- [Instructor] Processing forms is one of the most common things you'll probably do on your website in PHP. But before we get into actually processing the forms, there are two methods for processing that you should know about, GET and POST. This is associated with the method attribute in HTML, which determines how the form data should be sent. GET is the default option, this method transmits data from one page to another as part of the URL. So if I fill out this form, and then press Submit, you'll see that all of the data I just filled out shows up in the URL up here. I've also created the page so that it prints out the resulting array. To retrieve this information, you would need to use what's called a superglobal variable in PHP, and that's the GET array. This superglobal has all of the information passed to our action page, which since it's not defined defaults to the same page the form is on, it's passed as an associative array, which you can see here. The array has name, which is the name of the first form field and then the value, the same thing with email and reason. You'll also notice that topics which are checkboxes and the movies, which is the multiselect box are passed as arrays as well. So this is a multi dimensional array. Finally, you'll notice that the submit button is also passed. The other method is POST so if we change this method to POST and then let's add another condition up here, we'll say if not empty POST, var dump POST and then we will remove all of this, refresh and if we fill it out again and press Submit. Now it's the POST array that's printing. You'll notice that none of the information is passed in the URL here. So how do you choose which to use and when? With GET, the data is entered by the user and visible in the URL. So you should never pass sensitive data like passwords, you also shouldn't pass data that can be tampered with. So if I do something like, question mark hello equals there that is now in the get array. Using get and therefore allowing form data to be visible and mutable is great if you want to make the results shareable or if you want to let the user save them. But on the other hand, the length of a GET request is limited the limit can range between 2000 and 8000 characters depending on the server and browser configuration. There is no limit on results when using POST and it's more secure, but the results cannot be shared nor can the result of a specific form submission be saved without extra processing. So with that, let's get into processing some forms.

Contents