get,post Example of PHP form submission Data _php tutorial

Source: Internet
Author: User
Tags php form
This article to introduce an introductory tutorial on PHP form submission Data Get,post instance, there is a need to understand the students can enter the reference.

1. What are forms

The so-called form, said the simple point is a pair of form tags. That is:.

2. The role of the form

The purpose of the form is to collect the data submitted by the client and submit the data to the server.

For example, when you log in to a website, you need to enter your user name and password to log in. Another example is when you register the game account, you need to fill in your mailbox, password, age and so on.

These actions are submitted to the server through the form and are then recorded by the server in the database. (Not all)
3. Composition of the form

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

The form label has two required attributes: Action,method.

Action is to specify the address to be submitted to the server, such as I want to submit to the site of the info.php file, then write

Then create the info.php file, content:

The code is as follows Copy Code

$username = $_get["username"]; Get user Name

$password = $_get["password"]; Get password

Print output

echo "The user name you entered is:". $username. ", the password is:". $password;

?>

Friendship tip: All of the above quotation marks and semicolons are in the English state quotation marks and semicolons, the Chinese words will be error drops!! If you have Chinese quotation marks, please modify them yourself ^^


Understanding the Get and post of a form

In HTML, the form element uses the method property to specify two different methods of submission, the "Get" (the default value) and the "post".
1. Definition of Get and post

The HTML 4.01 specification says that the method property of the form element is used to specify the HTTP methods that send the form.

* When using GET, the data set of a form (a key-value pair, such as Control-name=current-value) is appended to the URI specified by the Action property of the form element;
* When using post, the data set of the form (such as the Control-name=current-value key-value pair) is wrapped in the requested body and sent.

This can be simply understood as that get is simply stitching a URI and then directly requesting data from the server (the dataset that needs to be submitted to the server is included in the URI). Like what:

This form will generate a GET request at the time of submission: Formget.aspx? Productid=1.

The post will wrap the data set of the form, that is, the key value pair of the productid=1 in the body of the request, send it to the server, and then request 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. The difference between get and post
2.1 Security

If you use get to submit a form that validates the user name and password, it is generally considered unsafe. Because the user name and password appear on the URL, it appears in the browser's history. Obviously, you should use post in case of security requirements.
2.2 Encoding

HTML 4.01 specification points out that get can only send ASCII characters to the server, while post can send characters from the entire ISO10646 (if enctype= "Multipart/form-data" is also specified).

Note that the Enctype property for Get and post corresponds to the difference. The enctype has two values, the default value is application/x-www-form-urlencoded, and the other value Multipart/form-data can only be used for post.
2.3 The length of the submitted data

The URL length is not limited by the HTTP specification, but IE restricts the requested URL length to 2,083 characters, limiting the data length of the get commit. The test shows that if the URL exceeds this limit, IE will not respond when the form is submitted. Other browsers do not have a URL length limit, so the length of data that other browsers can submit via get is limited only by the server's settings.

For post, because the data submitted is not in the URL, it is generally easy to assume that the data length limit is limited only by the server's settings.
2.4 Cache

Because a get result directly corresponds to a URI, the resulting page of get can be cached by the browser. The post is generally not, refer to 5.
2.5 References and SEO

For the same reason as above, we can use a URI to refer to a Get result page, and the result of post cannot, so must not be seo/seo.html "target=" _blank "> Search engine searched.
3. Service-side processing

In the service side of the ASP, 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. But this is less efficient than the former. We can use the following procedure to compare

The code is as follows Copy Code

Efficiency of request.querystring and request:
<%@ page language= "C #"%>

Request.querystring/request

The same way we can compare request.form and request.

Final results (Request.querystring[control-name]/request[control-name] and Request.form[control-name]/Request[ Control-name]) is less than 1 most of the time. Therefore, we should try to use request.querystring or Request.Form instead of Request.
4. Use Get and post correctly

The official advice of the web is to use get when and only if the form is idempotent (idempotent). Idempotent is a mathematical term, defined as: For a function f:d-D, if all x in D satisfies F (f x) = f x, then the function is idempotent. In HTTP specification (such as RFC 2616), the idempotent is interpreted as a side effect that occurs multiple times for the same request, and the same side effect as a single request.

For example, if you submit a form to query a keyword from Google, then we can assume that the form is idempotent, because the side effects of 1 commits and 10 commits are similar (10 queries may consume more power) If you submit a form to order an Ultimate Hornet (Utimate Bumblebee), then this is not idempotent: If you accidentally submitted more than 1 times the form, you may be scolded by the wife, you accidentally submitted 10 times, you may be bankrupt-- The side effects of one commit and multiple commits are significantly different, so this is not idempotent.

So, in general, if you submit this request simply to fetch the data from the server without doing anything else, and there are no obvious side effects for multiple commits, you should use get. Like what:

* Search engine query: http://www.google.com/search?q=yandixin;
* Paging: Articlelist.asp? Page=1.

If submitting this request will have other actions and effects, you should use post. Like what:

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

Another factor to consider is security. See 2.1.
5. Browser differences

* IE 6:url length is limited to 2,083 characters; after post, refreshing the page does not automatically re-post the data, there will be a warning;

Also, there is a possibility of "page has Expired" (usually post to yourself and then back up) during the retreat:

Microsoft's tech support staff claims to be "This isn't a bug or problem specified to the ASP. NET but a security feature of the IE Browser" and said "you can Al So inform your the users of this "is really absurd. In addition, a KB also mentions this issue, saying that the Response.CacheControl is set to "public" can be tested only for the first time back in effect.
* IE 7: Same as IE 6;
* Firefox 2.0.0.11: Refresh the page does not automatically re-post data, there will be a warning;
* Opera 9.24: normal (auto post data);
* After Safari 3.0.4:post, a warning appears when you refresh the page, forward, and backward without automatically re-post the data.

http://www.bkjia.com/PHPjc/628746.html www.bkjia.com true http://www.bkjia.com/PHPjc/628746.html techarticle This article to introduce an introductory tutorial on PHP form submission Data Get,post instance, there is a need to understand the students can enter the reference. 1. What is the form so-called form, said simple ...

  • 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.