Last Updated:

Creating a Post | Get in form with PHP

Beginners in programming begin their journey by studying php variables. Almost all useful information for site visitors is in variables, and the design of the page is in the template.

The programmer just needs to change the value of the variables and the user will receive an updated page.
Passing the values of variables is an integral part of the programmer's work, without knowledge of which it will not be possible to create a dynamic page.

Browser clients can send information to the server in two ways – this is one of the GET or POST methods. Before the browser sends information to the user, it encodes it using a scheme called URL encoding. In this scheme, name/value pairs are concatenated with an equal sign, and different pairs are separated by an ampersand:

name1=value1&name2=value2&name3=value3.

 

Get and post methods – send information to a web server

Typically, the browser communicates with the server using one of the above HTTP methods. They both convey information in their own way and have advantages and disadvantages. Let us dwell on them in more detail, considering each of them separately and then comparing them with each other.

GET Method

 

The data in the GET method is sent as URL parameters, which are sets of values and names separated by ampersands(&). Example of a URL with data:

http://www.example.com/action.php?name=vasya&age=33

Parts in bold are GET parameters, and parts that are italicized are get parameter values. More than one set can be embedded in a URL by combining them with ampersands (&). The GET method sends only plain text data.

Pros and cons of the GET method

  • The data sent by the GET method is reflected in the URL, which allows you to add a page with certain query string values to your favorites.
  • The username and password (confidential information) cannot be passed by the GET method. After all, they will be visible in the query string of the URL and can be stored in the browser's memory as a subsequent page.
  • The GET method assigns data to the server environment variable, so the URL length is limited. For example, there is a limit to the amount of data that can be sent.

To access all the information that is sent either through a URL or through a special PHP HTML form using method="get", PHP provides a super-global variable $_GET

<! DOCTYPEhtml>
<htmllang="en">
<head>
<title>An example of sending data from a form using the GET method</title>
</head>
<body>
<?php
if(isset($_GET["name"])){
echo "<p>Hi, " . $_GET["name"] . "</p>";
}
?>
<formmethod="get" action="<?phpecho $_SERVER["PHP_SELF"];? >»>
<labelfor="inputName">Name:</label>
<inputtype="text" name="name" id="inputName">
<inputtype="submit" value="Submit">
</form>
</body>
</html>

POST Method

The POST method sends data to the server as a batch in a separate message with a workflow. In this case, the data will not be shown in the URL.

Pros and Cons of the POST Method

  • The POST method is more secure because the information entered by users does not appear in the URL or in the server logs.
  • A larger limit on the amount of data transferred is proposed. In this case, it is possible to send text and binary data.
  • As already mentioned, posted data does not appear in the URL, so you can't bookmark a page with a specific request.
<! DOCTYPEhtml>
<htmllang="en">
<head>
<title>An example of sending data from a form using the POST method</title>
</head>
<body>
<?php
if(isset($_POST["name"])){
echo"<p>Hello, " . $_POST["name"] . </p>";
}
?>
<formmethod="post"action="<?phpecho $_SERVER["PHP_SELF"];? >»>
<labelfor="inputName">Name:</label>
<inputtype="text"name="name"id="inputName">
<inputtype="submit"value="Submit">
</form>
</body>
</html>

Variable $_REQUEST

PHP provides another super global variable, $_REQUEST, containing the values of the $_GET and $_POST variables and the values of the $_COOKIE super global variable.

<! DOCTYPEhtml>
<htmllang="en">
<head>
<title>$_REQUEST example</title>
</head>
<body>
<?php
if(isset($_REQUEST["name"])){
echo"<p>Hello, " . $_REQUEST["name"] . </p>";
}
?>
<formmethod="post"action="<?phpecho $_SERVER["PHP_SELF"];? >»>
<labelfor="inputName">Name:</label>
<inputtype="text"name="name"id="inputName">
<inputtype="submit"value="Submit">
</form>
</body>
</html>

Comparison of GET and POST methods

 

Data when using the GET method is passed by appending to the URL. So, they will be displayed, in terms of security it is bad. In addition, the maximum amount of data transmitted depends on the browser, namely on the maximum number of characters allowed in the address bar.

Example:

 

Post | Get in form with PHP

Using the POST method, the data will not be displayed in the browser, so they are more secure and the program that handles them is more secure in terms of security. At the same time, the amount of transmitted data is almost unlimited.

For clarity, we give a comparison in the form of a table:

SituationPOSTGET
Product FilterWhen visiting the page again, the user will have to re-fill out the filter form. Also, the page with parameters can not be shared with others, added to bookmarks and will return later without refilling.The visitor will receive a page with the necessary products, will be able to add them to bookmarks, share the link with others and return to the page after a while without having to re-fill out the form.

 

Authorization form 

 

Despite the fact that the data can be transmitted in unencrypted form, it can only be seen with the help of special software or through developer tools.

 

 

No data privacy protection. The password you enter will be available in the address bar of your Web browser. The specified password will be displayed in the browser bar and saved in the history of visits.

Online application

 

Ordering


Feedback form

 

 

Re-accessing the final link will not require reprocessing the request with the parameters filled in earlier.

In case of repeated access to the final link on the web server, reprocessing will occur, for example, a second order will be created, another request will be sent, and so on.

 

 

Via hyperlink There is no way to place a POST in a link.Clicking on a link with parameters is equivalent to sending a request through an HTML form.
AJAX requestsBoth methods are used. Their choice directly depends on the context.
 

When choosing a method of data transfer, it is necessary to take into account the above features and stop at the most suitable option.