What makes PHP unique

$ _GET and $ _POST

February 10, 2020
  1. Home
  2. »
  3. PHP tutorial
  4. »
  5. $ _GET and $ _POST

If you want to transfer values ​​from one page to the next in PHP, you often access the two special variables$ _GET and $ _POST back. For example, you want to transfer the user input of a form to your script, check it and save it. This user input is transmitted either by means of $ _GET or $ _POST.

Table of Contents

Data transfer via $ _GET

The GET method speaks of variable values ​​that are transferred using the URL. Perhaps you have already noticed in your browser that many URLs have a? after the file name have followed by corresponding values. These are the website's GET variables. You can access these in the PHP script as follows.

<?php $vorname = $_GET['vorname']; $nachname = $_GET['nachname']; echo "Hallo $vorname $nachname"; ?>

If you have this page on your web space using get.php? first name = Max & last name = Meier calls, you pass two $ _GET variables to the script. On the one hand the variable $ _GET ['first name'] with the value Max, on the other hand the variable $ _GET ['surname'] with the value Meier.

You can modify the call path of the interactive editor here on the page by clicking on the $ sign next to Run.

The GET variables begin after the? behind the name of your script. Different GET variables can be separated from each other with a corresponding & symbol. By the way, the order is not important, you can also use the script get.php? surname = Meier & first name = Max call.

Exercise: Extends the above script by a third GET variable, e.g. the middle name, pass this and let the value output.

GET variables are mostly used to provide links with corresponding dynamic values. Surely you've seen URLs that have a ? id = 456 or have other parameters accordingly. In this way, information is passed to the PHP script, for example which product from an online shop is to be displayed.


Data transfer via $ _POST

In contrast to $ _GET, $ _POST variables are not transferred via URL, but via form (more information on HTML forms). To do this, create an HTML form (page1.php) with the following content:

<form action="seite2.php"method="post">
First name:
<input type="Submit"value="Absenden" />

It is also important that we all Input fields unique Assign names so that they can be queried correctly on the second page after they have been sent. If two fields have the same name, only the latter field will be transferred, as the previous entry will be overwritten.

In the form above you have action the landing page is defined and at method the POST method selected. On page2.php you can now call up the entries of the form after submitting it as follows:

<?php $vorname = $_POST["vorname"]; $nachname = $_POST["nachname"]; echo "Hallo $vorname $nachname"; ?>

We remember that Input field with the name First name on page1.php, we now call this entry with $ _POST ["first name"]; from. Let's click on the form page Submitso will page2.php called up, the data is transmitted and output by it.

With $ _POST ["first name"]; so we can query the sent data again. ["First name"] indicates the name of the requested input field from the form page (here First name).

If you use our interactive editor and execute this code on our website, you can click on the $ symbol to display the GET and POST parameters that are to be transmitted to the code. In the example above, the two POST variables First name and Surname transmitted with the corresponding values. Unless it's locally available localhost tests, you need a corresponding form (page1.php), which sends the data to your page2.php.

Of course, $ _POST and $ _GET can be combined. First a form (page1.php):

<form action="seite2.php?wochentag=Sonntag"method="post">
First name:
<input type="Submit"value="Absenden"/>

The form above is the content of the form page2.php? weekday = Sunday transferred, i.e. a GET variable is also transferred. The data can then be called up on page2.php as follows:

<?php $vorname = $_POST["vorname"]; $nachname = $_POST["nachname"]; $wochentag = $_GET["wochentag"]; echo "Hallo $vorname $nachname. Treffen wir uns am $wochentag?"; ?>



$ _POST or $ _GET?

When do you use POST and when do you use GET? The question is easy to answer. If you want to transfer input from a form, you should always use POST.
Why ?
GET appends the entries to the URL, which limits the length of the text, and anyone in the browser history can see what was transmitted as data from the URL, and this is not so nice when entering a password.

Advantage of GET
GET is used when you want to pass simple information. If, for example, a selection is to be transferred by clicking on a link, then this method is used GET. A link in a calling page could then look like this:

<a href="artikel.php?id=234">Das PHP Einsteiger Buch</a>

In this example the input would be 234 be transmitted. Such links are used in PHP to program dynamic pages, e.g. to display product details in an online shop.

Author: Nils Reimers