PHP Lesson 8: send and receive data – $_GET and $_POST
If you want to transfer values in PHP from one page to the next, you often have to resort to the two special variables $ _GET and $ _POST. For example, you want to transfer the user input of a form to your script, check it and save it. The transfer of these user inputs takes place either by means of $ _GET or $ _POST.
Data transfer via $ _GET in PHP
The GET method refers to variable values that are passed using the URL. Maybe you have already noticed in the browser that many URLs? behind the file name have followed by corresponding values. These are the GET variables of the website. In the PHP script, you can access them as follows.
<? Php $firstname = $ _GET ['firstname']; $surname = $ _GET ['lastname']; echo "Hello $firstname $lastname"; ?>
If you call this page in your webspace via get.php?firstname=Robort&lastname=Gabriel, you give the script two $_GET variables. On the one hand the variable $ _GET [ ‘firstname’ ] with the value Robort, on the other hand, the variable $ _GET[ ‘lastname’] with the value Gabriel. The GET variables start after that? behind the name of your script. Different GET variables can be separated by & sign.
Incidentally, the order is not important, you can also use the script via get.php?lastname=Gabriel& firstname=Robort.
Exercise: Extends the above script by a third GET variable, eg the middle name, pass this and let the value be output.
GET variables are mostly used to provide links with correspondingly dynamic values. Surely you have already seen URLs that have an ?id=456 or other parameters. So you give the PHP script information, for example, which product should be displayed from an online shop.
Data transfer via $ _POST in PHP
Unlike $ _GET, $ _POST variables are transmitted via form rather than URL (more about HTML forms). Create an HTML form (page1.php) with the following content:
<form action="page2.php" method="post"> First name: <input type="text" name = "firstname"/> <br /> Name Name: <input type = "text" name = "last name" /> <br /> <input type = "Submit" value = "Submit" /> </form>
In the above form, the content of the form is transferred to page2.php?weekday=Sunday , ie an additional GET variable is transferred. The data can then be retrieved on page2.php as follows:
<?php $firstname = $_POST ["firstname"]; $surname = $_POST ["lastname"]; $weekday = $_GET ["weekday"]; echo "Hello $firstname $lastname. Are we meeting on the $weekday ?"; ?>
$ _POST or $ _GET? – PHP
When do you use POST and when do you use GET? The question is easy to answer. If you want to pass input from a form, you should always use POST.
Why do we use $_GET – PHP ?
With GET, the input of the URL is appended, thus the text length is limited, also everyone can recognize in the browser history at the URL, which was conveyed as data, and with a password input that is not so nice.
Advantage of $_GET – PHP
GET is used if you want to pass simple information. For example, if you want to pass a selection by clicking on a link, you use the method GET . A link in a calling page might look like this:
<a href=”article.php?id=234″> The PHP Beginner Book </a>
In this example, the input 234 would be transmitted. Such links are used in PHP for the programming of dynamic pages, eg to display product details in an online shop.
We are sorry that this lesson was not useful for you!
Let us improve this lesson!
Tell us how we can improve this lesson?