PHP form submit data get, post instance details _ PHP Tutorial

Source: Internet
Author: User
Tags php form php form submit
PHP form submit data get, post instance details. This article will introduce you to the get and post instances of PHP form submission data. For more information, see reference. 1. what is a form? Simply put, this article will introduce you to a Getting Started Tutorial on the get and post instance of PHP form submission data. For more information, see the reference.

1. what is a form?

A form is simply a form tag. That is :.

2. Functions of forms

A form collects data submitted by the client and submits the data to the server.

For example, when logging on to a website, you need to enter your username and password to log on. For example, when you register a game account, you must enter your email address, password, and age.

These operations are submitted to the server through a form, and are recorded in the database by the server. (Not all)
3. form composition

The two tags of the form must appear in pairs :.

The form tag has two required attributes: action and method.

Action is used to specify the address to be submitted to the server. for example, if I want to submit the info. php file to this site, I will write it

Create the info. php file. content:

The code is as follows:

$ Username = $ _ GET ["username"]; // GET the user name

$ Password = $ _ GET ["password"]; // GET the password

// Print the output

Echo "the user name you entered is:". $ username. ", and the password is:". $ password;

?>

Tip: All the quotation marks and semicolons are in the English format. if you are a Chinese character, an error is reported !! Modify the Chinese quotation marks ^


Understanding form get and post

In HTML, the form element uses the method attribute to specify two different submission methods, namely, "get" (default) and "post ".
1. get and post definitions

In W3C HTML 4.01 specification, the method attribute of the form element is used to specify the HTTP method for sending the form.

* When get is used, the form dataset (such as the control-name = current-value key-value pair) is appended to the URI specified by the action attribute of the form element;
* When post is used, the form dataset (such as the control-name = current-value key-value pair) is encapsulated in the request body and sent.

This can be simply understood as, get is just splicing a URI, and then directly request data from the server (the dataset to be submitted to the server is included in the URI ). For example:

When this form is submitted, it will generate such a get request: FormGet. aspx? ProductID = 1.

The post operation encapsulates the form dataset, namely the ProductID = 1, in the request body, and sends it to the server. then, it requests data from the server. For:

When such a form is submitted, we will see a clean URI: FormPost. aspx. Because the data is not spliced in the URI.
2. difference between get and post
2.1 Security

If you use get to submit a form that verifies the user name and password, it is generally considered unsafe. Because the user name and password will appear on the URL, and then in the browser's history. Obviously, post should be used when there are security requirements.
2.2 Encoding

HTML 4.01 specification indicates that get can only send ASCII characters to the server, while post can send all the characters in iso000046 (if both enctype = "multipart/form-data" is specified ).

Note that the enctype attributes of get and post are different. Enctype has two values. the default value is application/x-www-form-urlencoded, and the other value is multipart/form-data, which can only be used for post.
2.3 The length of the submitted data

HTTP specification does not limit the URL length, but IE limits the request URL length to 2083 characters, thus limiting the data length submitted by get. Tests show that if the URL exceeds this limit, IE will not respond when submitting the form. Other browsers do not have the URL length limit. Therefore, the length of data submitted by other browsers through get is limited by the server settings.

For post, because the submitted data is not in the url, it is generally considered that the data length is limited by the server settings.
2.4 cache

Because a get result corresponds to a URI directly, the get result page may be cached by the browser. Post is generally not supported. refer to 5.
2.5 references and SEO

For the same reason as above, we can use a URI to reference a get result page, while the post result cannot, therefore, it cannot be found by seo/seo.html "target =" _ blank "> search engines.
3. server processing

ASP. in the NET program, for get, we use Request. queryString [control-name] to obtain the corresponding = current-value; for post, we use Request. form [control-name].

We can also use Request [control-name] in general. However, this is not as efficient as the former. We can use the following program to compare

The code is as follows:

Efficiency of Request. QueryString and Request:
<% @ Page Language = "C #" %>

Script //<br />protected void Page_PreInit(object sender, EventArgs e)<br />{<br />if(Request["InputString"] != null)<br />{<br />int count = 1000000;<br />DateTime start;<br />DateTime end;<br />string value = &ldquo;&rdquo;;<br />start = DateTime.Now;<br />for(int i = 0;i < count;i++)<br />{<br />value = Request.QueryString["InputString"];<br />}<br />end = DateTime.Now;<br />double requestGet = (end &ndash; start).TotalSeconds;<br />start = DateTime.Now;<br />for(int i = 0;i < count;i++)<br />{<br />value = Request["InputString"];<br />}<br />end = DateTime.Now;<br />double request = (end &ndash; start).TotalSeconds;<br />compare.InnerHtml = requestGet.ToString() + &rdquo; / &rdquo; + request.ToString() + &rdquo; = &rdquo; + (requestGet / request).ToString();<br />get.InnerHtml = value;<br />}<br />}<br />// Script

Request. QueryString/Request

In the same way, we can compare Request. Form and Request.

The final result (Request. queryString [control-name]/Request [control-name] and Request. form [control-name]/Request [control-name]) is less than 1 in most cases. Therefore, we should try to use Request. QueryString or Request. Form instead of Request.
4. use get and post correctly

W3C official recommendation: Use get only when form is an idempotent (idempotent. Idempotence is a mathematical term defined as: For a function f: D-> D, if all x in D satisfies f (f x) = f x, this function is idempotent. In HTTP specification (for example, RFC 2616), idempotence is interpreted as the side effects of multiple identical requests, which are the same as the side effects of one request.

For example, if you submit a form and query a keyword from Google, we can think that this form is idempotent, because the side effects of one commit and ten commit are similar (10 queries may consume more energy). If you submit a form, you can order the Utimate bumblebee ), this is not idempotent: If you submit a form multiple times, you may be scolded by your wife. if you submit a form multiple times, you may be broke-the side effects of one commit and multiple submissions are significantly different, so this is not idempotent.

Therefore, if you submit a request to retrieve data from the server without performing other operations, and submit the request multiple times without obvious side effects, you should use get. For example:

* Search engine query: http://www.google.com/search? Q = yandixin;
* Page: ArticleList. asp? Page = 1.

If you submit this request, other operations and impacts will occur, you should use post. For example:

* Modify the data in the database on the server;
* Send an email;
* Delete an object.

Another factor to consider is security. See Figure 2.1.
5. browser differences

* IE 6: The URL length is limited to 2083 characters. after The post operation, the page will not be refreshed and the data will not be automatically re-posted, and a warning will appear;

In addition, "Page has Expired" may appear in the backend process (usually post to yourself and then back ):

Microsoft technical support personnel claim that "this is not a bug or problem specified to the ASP. NET but a security feature of the IE Browser ", and said" You can also inform your users of this ", it is really absurd. In addition, KB also mentioned this issue, which means you can set Response. CacheControl to "Public". it is only valid after the first rollback.
* IE 7: same as IE 6;
* Firefox 2.0.0.11: refreshing the page does not automatically post data again, and a warning is reported;
* Opera 9.24: Normal (automatic post data );
* Safari 3.0.4: After a post request, the system will not automatically repost the data after the page is refreshed, forward, or backward, and a warning will appear.

Bytes. 1. what is a form? simple form...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.