$_POST or $_GET Method

5 min read

If you want to transfer values ​​in PHP from one page to the next one, you often have to resort to the two special variables method $_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 is done either by using $_GET or $_POST method.

Transferring data via $_GET method

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'];
$lastname = $_GET['lastname'];
echo "Hello $firstname $lastname";
?>

If you call this page on your web space using get.php?firstname=Wira&lastname=Tama, you pass two $_GET variables to the script. On the one hand the variable $ _GET[ 'firstname' ] with the value Wira, on the other hand, the variable $ _GET[ 'last name'] with the value Tama.

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

The GET variables start after that '?' behind the name of variable get url (firstname). An '&' sign can separate different GET variables. Incidentally, the order is irrelevant. You can also call the script using get.php?lastname=Tama&firstname=Wira.

Exercise: Extends the above script by a third GET variable, e.g., 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.

Transferring data via $_POST Method

Unlike $_GET, $_POST variables method are transmitted via form rather than URL. Create an HTML form (page1.php) with the following content script:

<form  action  =  "page2.php"  method = "post" >
First name: <input type = "text" name = " firstname " /> <br />
Name: <input type = "text" name = "lastname" /> <br />
<input type = "submit" value = " Submit " />
</form>

It is also important that we have to assign a unique name to all input fields so that they can also be queried correctly after being sent on the second page. If two fields have the same name, only the latter field will be transmitted because the previous entry is overwritten.

In the form above, you defined the landing page for action and selected the POST method for the method. On page2.php you can retrieve the input of the form after submitting as follows:

<?php
$firstname = $_POST ["firstname"];
$lastname = $_POST ["lastname"];
echo "Hello $firstname $lastname";
?>

We remember the input field with the name firstname on page1.php, which we call now with $_POST ["firstname"];. If we click Submit button on the form page, then page2.php will be called, the data will be transmitted and output them.

With the $_POST ["firstname"];, We can save the sent data via form submit. ["firstname"] indicates the name of the requested input field from the form page (here firstname).

If you use our interactive editor and run this code on our website, clicking on the $ symbol will show you the GET and POST parameters to be passed to the code. In the above example, the two POST variables are given the first name and last name with the corresponding values. If you test it locally on localhost, you will need a form (page1.php) which will send the data to your page2.php.

Of course, you can combine $_POST and $_GET all together. Below is the modified first page (page1.php) content:

<form  action  =  "page2.php?weekday=sunday"  method = "post" >
First name : <input type = "text" name = "first name" /> <br />
Name Name : <input type = "text" name = "surname" /> <br />
<input type = "submit" value = " Submit " />
</form>

In the form above, the content of the form is transferred to page2.php?weekday=sunday, in conjunction with additional GET variables. The data can then be retrieved on page2.php as follows:

<?php
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$weekday = $_GET["weekday"];
echo "Hello $firstname $lastname. Are we meeting on the $weekday?";
?>

$_POST or $_GET ?

When do you use POST and when do you use GET method? The question is easy to answer. If you want to pass input from a form, you should always use POST.

Why?

With GET, the input of the URL is appended. Thus the text length is limited. Also, everyone can recognize it in the browser URL bar, which was conveyed as data, and with a password input. Therefore, that is not a best practice.

Advantage of GET Method

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 the method GET is used. A link in a calling page might look like this:

 <a href="artikel.php?id=234"> The PHP Beginner</a>

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

« »