Understand the GET and POST submission methods in the form tag in ten minutes

WBOY
Release: 2021-12-27 19:00:56
forward
6878 people have browsed it

This article brings you relevant knowledge and comparison of the two submission methods of get and post in the form tag. The function of the form form is to collect the content in the tag,

...
In the middle, visitors can add text, selections, or some control modules, etc. Then these contents will be sent to the server. I hope it will be helpful to everyone.

Understand the GET and POST submission methods in the form tag in ten minutes

GET and POST in the form tag

In HTML, the function of the form form is to collect the content in the tag,&lt ;form>... In the middle, the visitor can add text, selections, or some control modules, etc. Then these contents will be sent to the server.

A form must specify two things:

  1. The method parameter of form is used to set the submission method of the form, and POST is used by default.
  2. action is used for Set the submission URL of the form. If you do not write it or keep an empty string, the current URL will be used.

①Example of form submission using post method:

Below Example implementation process:
 When we access this interface for the first time, it is the GET method (accessing a URL in the browser is a GET method, no need to explain). Observing the view function, we can see that it renders the form to the user. template page.
When we enter data in the input box and click submit, a POST method will be sent, so that according to the view function, the data entered in the input box will be printed on the console.

Note:

  1. The post submission method will not display parameters in the URL;
  2. You can obtain the submitted information through request.POST.get data.

Understand the GET and POST submission methods in the form tag in ten minutes
Understand the GET and POST submission methods in the form tag in ten minutes

Understand the GET and POST submission methods in the form tag in ten minutes

②Example of form submission using get method:

The following example implementation process:
 When we access this interface for the first time, it is the GET method (accessing a URL in the browser is a GET method, no explanation is needed). Observing the view function, we can see that it renders to the user with The form template page.
When we enter data in the input box and click submit, the GET method will be sent (because we set the POST submission method in the form form), so that according to the view function, the input box input will be printed on the console The data.

(Because of our settings, clicking the submit button in the template is a GET submission, and the values ​​of a and b submitted by the form form can be printed on the corresponding terminal.)

Note:

  1. getThe submitted parameters will be displayed in the url;
  2. You can obtain the submitted parameters through the request.GET.get method.

Understand the GET and POST submission methods in the form tag in ten minutes

Understand the GET and POST submission methods in the form tag in ten minutes

Understand the GET and POST submission methods in the form tag in ten minutes

##③One-click multi-value getlist method:

The attributes GET and POST of the request object are both QueryDict type objects; Different from python dictionaries, QueryDict type objects are used to handle situations where the same key has multiple values.

    Method get():
  • Get the value according to the key, you can only get one value of the key
    If a key has multiple values ​​at the same time, get the last value (because Covered!)
  • Method getlist():
  • Get the value according to the key and return the value of the key in a list
    You can get multiple values ​​of a key

For example: How does the backend get the options selected by the user in the multi-select box - use the getlist method!
Understand the GET and POST submission methods in the form tag in ten minutes

Understand the GET and POST submission methods in the form tag in ten minutes

④Attributes of GET and POST objects in request:

First: GET attribute!

  • Object of QueryDict type
  • Contains all parameters of the get request method
  • Corresponds to the parameters in the url request address, located after ?
  • The format of the parameters is a key-value pair, such as key1=value1
  • Use & to connect multiple parameters, such as key1=value1&key2=value2

The second one: POST attribute!

  • QueryDict type object
  • Contains all parameters of the post request method
  • Corresponds to the controls in the form form
  • The controls in the form must have name attribute, then the value of the name attribute is the key, and the value of the value attribute is the value, forming a key-value pair submission
  • For the checkbox control, the name attribute is also a group, and will be submitted when the control is selected. There is One-click multi-value situation.

Small extension:

Construct a GET request-as long as we click the 'click' button, we will find that the function is the same as "②form form usage" "get method" has the same effect. We can also print the values ​​of a and b on the backend (you can also see it by observing the URL link in the browser!), indicating that the data submission is successful!
Understand the GET and POST submission methods in the form tag in ten minutesUnderstand the GET and POST submission methods in the form tag in ten minutes

⑤ Summary of GET and POST request methods:

  1. GET: GET, as its name suggests, obtains data from the server and does not change the server. The status and data are sent to the server carrying parameters in the URL.
  2. POST sends a certain amount of data to the server, usually changing the server's data.
  3. The parameters of the POST method cannot be seen in the URL. They are passed to the server through the body parameters. Therefore, compared with the GET method, you can directly see the passed parameters in the URL, which is safer. Of course, it is also It cannot be simply judged that the POST method is more secure than the GET method. To keep the website safe, more security processing needs to be done.

Recommended tutorial: "html video tutorial"

The above is the detailed content of Understand the GET and POST submission methods in the form tag in ten minutes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template