PHP form submission data get and post instance details

Source: Internet
Author: User
Tags php form

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?

A form is simply a form tag. That is, <form> </form>.

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: <form> </form>.

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, I want to submit the address to info on this site. PHP file, then it is written as <form action = "http: // www. /info. php ">

The file on this site can be written to the relative path: <form action = "info. php">

The method attribute, as its name implies, is the method for data submission. It has two optional values: post/get, post is hidden transfer, and get is transmitted in the address bar. For details about the differences, see get and post in form.

If get is used for transmission, the above Code can be written as: <form action = "info. php" method = "get">

You can add a text box to the form tab, for example, if you enter the user name and password:

Username: <input type = "text" name = "username" value = ""/>

Password: <input type = "password" name = "password" value = ""/>

You can also use multiple lines of text such as textarea:

<Textarea name = "content"> </textarea>

Add a submit button and a reset button:

<Input type = "submit" value = "submit"/>

<Input type = "reset" value = "reset"/>

As you can see, in addition to submitting, resetting, and form, the preceding labels have some name attributes, because the server obtains their values through the name attribute of the tag.

4. How can I obtain data in info. php?

There are three methods: $ _ GET, $ _ POST, and $ _ REQUEST.

$ _ GET can only obtain the data submitted by the get method, and the $ _ POST method can only obtain the data submitted by the post method, while $ _ REQUEST can simultaneously obtain the data submitted by the get and post methods.

For example, to obtain the user name and password value entered by the user, it is $ _ GET ["username"] and $ _ GET ["password"]. or $ _ REQUEST ["username"] and $ _ REQUEST ["password"]

The following is the instance code:

Create a test.html file with the following content:

The Code is as follows: Copy code

<Form action = "info. php" method = "get">

Username: <input type = "text" name = "username" value = ""/> <br/>

Password: <input type = "password" name = "password" value = ""/> <br/>

<Input type = "submit" value = "submit"/>

<Input type = "reset" value = "reset"/>

</Form>

Create the info. php file. content:

The Code is as follows: Copy code

<? Php

$ 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:

<Form action = "FormGet. aspx" method = "get"> <input name = "ProductID" type = "text" value = "1"/>
<Input type = "submit" value = "Get"/>
</Form> when the form is submitted, 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:

<Form action = "FormPost. aspx" method = "post"> <input name = "ProductID" type = "text" value = "1"/>
<Input type = "submit" value = "Get"/>
</Form> 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: Copy code

Efficiency of Request. QueryString and Request:
& Lt; % @ Page Language = "C #" % & gt;

<Script> // <! [CDATA [
Protected void Page_PreInit (object sender, EventArgs e)
{
If (Request ["InputString"]! = Null)
{
Int count = 1000000;
DateTime start;
DateTime end;
String value = "";
Start = DateTime. Now;
For (int I = 0; I <count; I ++)
{
Value = Request. QueryString ["InputString"];
}
End = DateTime. Now;
Double requestGet = (end-start). TotalSeconds;
Start = DateTime. Now;
For (int I = 0; I <count; I ++)
{
Value = Request ["InputString"];
}
End = DateTime. Now;
Double request = (end-start). TotalSeconds;
Compare. InnerHtml = requestGet. ToString () + "/" + request. ToString () + "=" + (requestGet/request). ToString ();
Get. InnerHtml = value;
}
}
//]> </Script>

Request. QueryString/Request

<Form action = "FormGet. aspx" method = "get">
<Div> <input name = "InputString" type = "text"/> <input type = "submit" value = "Post"/>

Get: <span id = "get"> </span>

Request. QueryString/Request: <span id = "compare"> </span> </div>
</Form>

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-& gt; 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.

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.