What is Get and POST in HTML. Attribute Action and Method
The use of GET and POST methods in PHP is difficult to overestimate, since these methods are found almost on each site. Before studying the described material below, I advise you to get acquainted with HTML tag
Values
The value of the Method attribute does not depend on the register. There are two methods - GET and POST.
Get this method is one of the most common and designed to obtain the required information and data transmission in address line. The "Name \u003d value" pairs are joined in this case to the address after the question mark and are separated by the ampersand (symbol &). Ease of use of the GET method is that the address with all parameters can be used repeatedly by saving it, for example, in the browser bookmarks, as well as change the parameter values \u200b\u200bdirectly in the address bar. POST POST Method sends data to the server in the browser request. This allows you to send a larger amount of data than the Get method is available, since it has a limit in 4 KB. Large data volumes are used in forums, postal services, filling the database, when sending files, etc.
Mandatory attribute
Default value
HTML5 IE CR OP SA FX
This post is intended to explain the principles of data transfer on the Internet with the help of two main methods: Get and Post. I wrote it as a supplement to the instructions for the generator of a changeable schedule of work for those who are hardly interesting for the details ☺.
Go to the following address (this is for a visual explanation): http://calendarin.net/calendar.php?year\u003d2016 Please note the address bar of the browser: calendarin.net/calendar.php ? year \u003d 2016 The main file is called, followed by a question mark (?) And the "Year" parameter with the value "2016". So, everything that follows a question mark, this is a get-request. Everything is simple. To transmit not one parameter, but a few, then they need to divide the ampersand (&). Example: calendarin.net/calendar.php. ? year \u003d 2016 & Display \u003d Work-Days-and-Days-off
The main file is also called, followed by a question mark (?), Then the "Year" parameter with the value "2016", then ampersand (&), then - the "Display" parameter with the "Work-Days-and-Days" parameter -OFF.
Get-parameters can change directly in the browser address bar. For example, changing the value "2016" to "2017" and pressing the key, you will go to the calendar for 2017.
This data transmission in a hidden way (the page address does not change); That is, to see what was transmitted, you can only using the program (script). For example, in the following tool for calculating characters in the text, the initial data is transmitted by the POST method: http://usefulonlinetools.com/free/character-counter.php
If you have questions, comments and my e-mail is at your service.
In addition to the GET method, which we reviewed in the previous note, there is another method of sending a query on the HTTP protocol - the POST method. The POST method is also very often used in practice.
If, in order to refer to the GET method, it is enough for us to type the request to the URL, then everything works in the POST method on another principle.
In order to execute this type of request, we need to click on the button with the Type \u003d "submit attribute", which is located on the web page. Please note that this button is located in the element.
Consider this HTML code:
![](https://i0.wp.com/steptosleep.ru/wp-content/uploads/2018/06/92221.png)
If the user enters some text to the text field and clicks on the "Submit" button, then the TEXT variable will be sent to the server with the content of the content that the user entered.
POST and GET requests with simple words
This variable will be sent by the POST method.
If you write in the form:
That data will be sent by the GET method.
If, in the case of a get request, the amount of data that we could transmit is limited to the browser length length, then in the case of the post request, there is no such restriction, and we can transmit significant amounts of information.
Another difference of the POST method from GET, the POST method hides all the variables transmitted to them and their values \u200b\u200bin its body (Entity-Body). In the case of the GET method, they were stored in the query string (Request-URI).
Here is an example of a request made by the POST method:
POST / HTTP / 1.0 \\ R \\ N
HOST: www.site.ru \\ r \\ n
Referer: http://www.site.ru/index.html\\r\\n.
Cookie: Income \u003d 1 \\ R \\ n
Content-Type: Application / X-WWW-FORM-URLENCODED \\ R \\ N
Content-Length: 35 \\ R \\ N
\\ r \\ n
Login \u003d Dima & Password \u003d 12345
Thus, passing data by the POST method, they will be much more difficult to intercept the attacker, because They are hidden from direct viewing, so the method of transmitting data by the POST method is considered to be more secure.
In addition, the POST method can be transmitted not only text, but also multimedia data (pictures, audio, video). There is a special content-type parameter that defines the type of information to be transmitted.
And finally, to get the data that were transmitted by this method, the POST variable is used.
Here is an example of processing in PHP:
echo $ _post ['text'];
?>
In the last note, we decided that the browser (client) sends requests to the HTTP server, and the server sends an HTTP response to the HTTP client. These requests and answers are issued according to certain rules. There is something like syntax, as in which sequence should be written. There must be a strictly defined structure.
Let us consider this structure in more detail, which builds requests and answers in the HTTP protocol.
HTTP request consists of three main parts that go in it in the order that is listed below. There is an empty line between the headers and body of the message (as a separator), it is a string translation symbol.
Empty string (separator)
POST and GET requests, what is the difference between them and what is better and for what purposes?
message Body (Entity Body) - optional parameter
Row request - Indicates the transfer method, the URL address to which the HTTP protocol version needs to be accessed.
Headlines - describe the message body, transmit different parameters and other information and information.
message body - This is the data that is transmitted in the request. The message body is an optional parameter and may not be missing.
When we get a response from the server, the message body is most often the contents of a web page. But, when queries to the server, it can also sometimes be present, for example, when we convey the data that was filled in the feedback form to the server.
In more detail, each query element, we will look at the following notes.
Let's, for example, consider one real request to the server. I highlighted each part of the query by my color: a query line - green, headlines - orange, the body message is blue.
Request from browser:
Host: Webgyry.info.
Cookie: WP-SETTINGS
Connection: Keep-Alive
The following example already has a message body.
Server Answer:
Content-Type: text / html; Charset \u003d UTF-8
Transfer-encoding: chunked
Connection: Keep-Alive
Keep-Alive: Timeout \u003d 5
X-pingback: //webgyry.info/xmlrpc.php
Here are the messages of the client and the HTTP server.
By the way, do you want to know whether it makes sense in some item on your site using the "goals" of Yandex Metrics and Google Analytics?
Remove what does not work, add what works and double your revenue.
Course on setting the goals of Yandex Metrics ..
Google Analytics Goals Setup Course ..
HTTP client sends a request to the server in the form of a request-request, which has the following format:
- Query Row (Mandatory Element)
- Title (optional element)
- Empty string (mandatory element)
- Message body (optional element)
Consider each of these elements separately.
Row request
The query string begins with a method token, after which the URI query and the protocol version follow. Elements are scattered from each other spaces:
Consider this item in more detail
Request method
This element indicates a method that must be called on the server side to the specified URI inferction.
There are eight methods in HTTP:
- Head.
Used to get the status string and header from the URI server. Does not change the data. - Get.
Used to receive data from the server to the specified URI. Does not change the data. - POST.
Used to send data to the server (for example, information about the developer, etc.) using HTML forms. - Put.
Replaces all previous data on the resource new downloaded data. - Delete.
Removes all current data on a resource defined by URI. - Connect.
Sets the tunnel connection to the server according to the specified URI. - Options.
Describes the connection properties for the specified resource. - Trace
Provides a message containing the return trace of the resource specified in the URI.
URI request
URI (Uniform Resource Identifier) \u200b\u200bis a resource identifier to which the query is sent. Below is the most common URI format:
‘*’ Used when an HTTP request does not apply to a specific resource, but to the server. Used only in the case when the method is not necessary to apply to the resource. For example,
absoluteuri. Used when an HTTP request is performed on a proxy. Proxy is requested to transmit a query from an affordable cache and returns the answer. For example:
Asbutivated_Put | a sourcethe most Chatso is used.
Learning to work with Get and Post Requests
A specific resource of a specific server is requested. For example, the client wants to get a resource from the server through the 80th port. The resource address "www.proselyte.net" and sends the following request:
Request header fields
The header fields allow the client to transfer additional information about the request and about themselves to the server itself. These fields act as query modifiers.
Below is the intersection of the most important header fields that can be used:
- Accept-Charset.
- Accept-Encoding
- Accept-Language.
- Authorization
- Expect
- IF-Match.
- IF-Modified-Since
- IF-None-Match
- IF-Range.
- IF-Unmodified-Since
- Range.
- Referer.
- User-Agent
If we decide to implement your own client and your own web server, then we can create your own header fields.
An example of http request
On this we finish learning HTTP requests.
In the next article, we will look at HTTP answers.
One way to send a request via HTTP to the server is to request the GET method. This method is the most common and requests to the server most often occur with its use.
The easiest way to create a Get query can be used to type the URL in the address bar of the browser.
The browser will send the server for approximately the following information:
Get / http / 1.1
Host: Webgyry.info.
User-Agent: Mozilla / 5.0 (Windows NT 6.1; RV: 18.0) GECKO / 20100101 Firefox / 18.0
Accept: text / html, application / xhtml + xml, application / xml; q \u003d 0.9, * / *; q \u003d 0.8
Accept-Language: RU-EN, RU; Q \u003d 0.8, EN-US; Q \u003d 0.5, EN; Q \u003d 0.3
Accept-Encoding: Gzip, Deflate
Cookie: WP-SETTINGS
Connection: Keep-Alive
The request consists of two parts:
1. Request Line (Request Line)
2. Headers (Message Headers)
Please note that the GET query does not have a message body. But, this does not mean that with it, we cannot transfer any information to the server.
Difference between GET and POST methods
This can be done with the help of special GET parameters.
To add GET parameters to request, you need to put a sign "?" At the end of the URL address And after it to start setting them at the following rule:
parameter_name1 \u003d Value Parameters1 & Parameter Name2 \u003d Value Parameters2 & ...
The separator between the parameters is the "&" sign.
For example, if we want to pass two values, the username and its age, it can be done next line:
http://site.ru/page.php?name\u003ddima&age\u003d27
When this query is executed, the data fall into the so-called query_string environment variable from which they can be obtained on the server using the web programming server.
Here is an example, as can be done on PHP.
Echo "Your name:". $ _Get ["Name"]. "
»;
Echo "Your age:". $ _Get ["Age"]. "
»;
?>
The $ _GET design ["parameter name"] allows you to display the value of the transmitted parameter.
As a result of the execution of this code in the browser will be displayed:
Your name: Dima
Your age: 27
we also perform a request to the GET server.
You could notice that the addresses can be contemplated on most sites:
Http: //syt/index.php? Blog \u003d 2
Here, not even knowing PHP, you can guess that we appeal to the file index.php. But what goes after the question of the question, few people know. Everything is quite simple: ? blog \u003d 2 This is an announcement of the global variable "$ _Get [" Blog "]" with the value "2". Thus, I transmit a variable to the script that is responsible for output from the database. Let's write a small script in which you will only clearly see:
if (isset ($ _ get ["blog"])) (
echo $ _get ["blog"];
}
?>
We use the IF () condition operator () as a condition worth this line:
Isset ($ _ get ["blog"])
isset () Allows you to find out if there is a variable that is listed in brackets, that is, the condition that I described in the code sounds like this: if there is a variable $ _Get ["blog"] then output the contents of this variable on the screen. That's what happened:
I think the global variable is clearly created. $ _Get. with the identifier that we have announced in the address bar ( in this case, with the "Blog" identifier)
Now I want to clarify one moment. Suppose we need to declare two variables, how to do it? The first variable is declared after the question of the question "?" The second variable is declared after such a sign "&" ( To be honest I do not know what kind of sign ), here is an example announcement of three variables:
Http: //syt/index.php? A \u003d 1 & b \u003d 2 & c \u003d 3
Here is the output code:
if (ISset ($ _ get ["a"]) and isset ($ _ get ["b"]) and isset ($ _ get ["C"])) (
Echo $ _GET ["A"]. "
";
Echo $ _get ["b"]. "
";
Echo $ _Get ["C"]. "
";
}
?>
The condition sounds like this:
If there is a global variable $ _GET ["A"] and the global variable $ _Get ["b"] and the global variable $ _Get ["C"] then output them on the screen, Here is the result:
Forms
Before we go to pOST. Requests, you need to disassemble what form are? Why do you need? Because the global variable $ _post ["] is created through forms. What is a form? These are fields to enter any information of the user. Fields are in one line, large fields, there are radio buttons, check boxes. We will analyze everything in order ...
Form is a tag:
Elements of form
The form has attributes, list the most common:
Let's create a form:
Elements of form
I set the file as a handler file test.php. Since it is in it that I am writing examples for you. I put the POST method as it is these methods that use in 99.9% of cases. I also assigned our form name - Form
Now plunge into the world of form elements. First of all, you must understand that almost all elements are tag Difference only in the attribute type These tags. Let me list the forms you used:
I am sure that you have not met such fields, so here as it says: "no comments"
Now let's make a small training questionnaire with which we will work further. Our task is to make a small form that tells us the name of the fill, the floor, from what country it is, favorite color and field of text where the user can add something about himself. That's what I did:
Your last name name Patronymic:
Your gender:
M.
J.
What country are you from
Favorite color (a):
The black:
Red:
White:
Other:
About myself:
Please note that almost every tag has an attribute value.What is he? It records the data you are going to transfer to another page. I hope it is clear
Now if you run this code in the browser, we will see the following:
Form I used an attribute action. with meaning test.php. This means how I have already said that the data from the form is sent to the Test.php file.
POST request
Now write php code which will allow us to see the information you entered. Where is the data stored? In the case of the GET request, our data was lying in the $ _get global variable]. With a post request, the data will lie in the global variable $ _post ["]. In square brackets, it is necessary to prescribe, as in the case of the GET global variable, an identifier. The question is where this identifier take? That's why NAME attribute is needed at the form elements! It is these names that serve us with the key in the global array of POST. Well, let's start the script description:
if (ISset ($ _ post ["submit"])) (
Echo "FULL NAME:" $ _ POST ["FIO"]. "
";
Echo "Gender:" $ _ POST ["SEX"]. "
";
Echo "Country of residence:" $ _ post ["City"]. "
";
Echo "Favorite color (a):
";
Echo $ _Post ["Color_1"]. "
";
Echo $ _post ["Color_2"]. "
";
Echo $ _Post ["Color_3"]. "
";
Echo $ _Post ["Color_4"]. "
";
Echo "About me:" $ _ post ["About"]. "
";
}
?>
The IF condition written is written: if there is a global variable $ _post ["submit"], then display data on the screen. This global variable is created if we click on the Send button, for which this example requires the NAME attribute in the button. You can quite wonder why the button attribute NAME is not required? Everything is quite simple. Usually the programmer does not track the press of the button, and tracks the sent data. For correct operation, for example, the contact form, you must track down without pressing the button, but the correctness of the input of information, and learn whether this information was introduced at all. In our example, we did not check the sent data, and simply traveled to press the button, to simplify the example ... That's what we did:
Conclusion
Well, for today we have disassembled two methods of data transfer between the scripts, and gallopam familiar with the forms. I really hope that this information you have at least somewhere useful. If you have any questions, or thoughts, write comments. Good luck to you, today I have everything!
P.S.: Would you like computer games Have become even more realistic? DirectX 11 For Windows 7 You can download free on Windows in! Enjoy wonderful graphics!