Detailed explanation of get and post examples of PHP form submission data_PHP tutorial

WBOY
Release: 2016-07-13 17:16:02
Original
1709 people have browsed it

This article will introduce you to an introductory tutorial on get and post examples of submitting data in PHP forms. Students who need to know more can refer to it.

1. What is a form

The so-called form, to put it simply, is a pair of form tags. That is:

.

2. The function of the form

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

For example, when logging into a website, you need to enter your username and password to log in. Another example is when you register a game account, you need to fill in your email, password, age, etc.

These operations are submitted to the server through a form, and are finally recorded in the database by the server. (not all)
3. Composition of the form

The two tags of the form must appear in pairs:

.

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

The function of action is to specify the address to be submitted to the server. For example, if I want to submit the info.php file to this site, then I would write

, just write relative paths to the files on this site:

The method attribute, as the name suggests, is the method of submitting data. It has two optional values: post/get. Post is a hidden transfer, and get is an address bar transfer. For specific differences, please refer to the article "Detailed analysis of the differences between form get and post".

If passed by get, the above can be written as:

You can add a text box in the form tag, for example, for the user to fill in the user name and password:

Username:

Password:

You can also use multi-line text area, etc.:

Finally add a submit button and a reset button:

Careful children can see that in addition to submit, reset and form forms, the above tags all have name attributes, because the server obtains its value through the name attribute of the tag.

4. So how to get data in info.php?

There are three methods: $_GET, $_POST, $_REQUEST.

$_GET can only get the data submitted by the get method, the $_POST method can only get the data submitted by the post method, and $_REQUEST can get the data submitted by both the get and post methods.

For example, to get the values ​​of the username and password entered by the user, it is $_GET["username"] and $_GET["password"], or $_REQUEST["username"] and $_REQUEST["password"]

The following is the example code:

Create test.html file, content:

The code is as follows Copy code
 代码如下 复制代码

用户名:

密    码:

Username:
Password:

Create the info.php file again, content:

The code is as follows Copy code
 代码如下 复制代码

$username = $_GET["username"]; //获取用户名

$password = $_GET["password"]; //获取密码

//打印输出

echo “您输入的用户名为:” . $username . “,密码为:”. $password ;

?>

<🎜>$username = $_GET["username"]; //Get username<🎜> <🎜>$password = $_GET["password"]; //Get password<🎜> <🎜>//Print output<🎜> <🎜>echo "The user name you entered is:" . $username . ", the password is: ". $password;<🎜> <🎜>?>

友情提示:以上所有引号和分号均为英文状态下的引号和分号,中文的话会报错滴!!如有中文引号请自行修改 ^^


理解form的get和post

在HTML中,form元素用method属性来指定有两种不同的提交方法,即”get”(默认值)和”post”。
1. get和post的定义

W3C的HTML 4.01 specification说,form元素的method属性用来指定发送form的HTTP方法。

* 使用get时,form的数据集(形如control-name=current-value的键值对)被附加到form元素的action属性所指定的URI后面;
* 使用post时,form的数据集(形如control-name=current-value的键值对)被包装在请求的body中并被发送。

这可以简单地理解为,get仅仅是拼接一个URI,然后直接向服务器请求数据(需要提交给服务器的数据集包含在URI中)。比如:



这个form在提交的时候,会产生这样能够一个get请求:FormGet.aspx?ProductID=1。

而post会把form的数据集,即ProductID=1这个键值对包装在请求的body中,发送给服务器,然后向服务器请求数据。对于:



这样一个form在提交时,我们将看到一个干净的URI:FormPost.aspx。因为数据不是拼接在URI中。
2. get和post的区别
2.1 安全性

如果用get提交一个验证用户名和密码的form,一般认为是不安全的。因为用户名和密码将出现在URL上,进而出现在浏览器的历史记录中。显然,在对安全性有要求的情况下,应该使用post。
2.2 编码

HTML 4.01 specification指出,get只能向服务器发送ASCII字符,而post则可以发送整个ISO10646中的字符(如果同时指定enctype=”multipart/form-data”的话)。

注意get和post对应的enctype属性有区别。enctype有两个值,默认值为application/x-www-form-urlencoded,而另一个值multipart/form-data只能用于post。
2.3 提交的数据的长度

HTTP specification并没有对URL长度进行限制,但是IE将请求的URL长度限制为2083个字符,从而限制了get提交的数据长度。测试表明如果URL超出这个限制,提交form时IE不会有任何响应。其它浏览器则没有URL的长度限制,因此其它浏览器能通过get提交的数据长度仅受限于服务器的设置。

而对于post,因为提交的数据不在url中,所以通常可以简单地认为数据长度限制仅受限于服务器的设置。
2.4 缓存

由于一个get得到的结果直接对应到一个URI,所以get的结果页面有可能被浏览器缓存。而post一般则不能,参考5。
2.5 引用和SEO

出于和上面相同的原因,我们可以用一个URI引用一个get的结果页面,而post的结果则不能,所以必然不能被seo/seo.html" target="_blank">搜索引擎搜到。
3. 服务端的处理

在服务端的ASP.NET程序中,对于get,我们用Request.QueryString[control-name]来取得对应的=current-value;对于post,我们用Request.Form[control-name]。

我们也可以笼统地使用Request[control-name]。但这样做的效率不如前者。我们可以用下面的程序比较

 代码如下 复制代码

Request.QueryString和Request的效率:
<%@ Page Language=”C#” %>

<script>// <![CDATA[<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


Get:

Request.QueryString / Request:


同样的办法我们可以比较Request.Form和Request。

最后得到的结果(Request.QueryString[control-name] / Request[control-name]和Request.Form[control-name] / Request[control-name])大多数时候是小于1的。因此,我们因该尽量用Request.QueryString或 Request.Form来代替Request。
4. 正确地使用get和post

W3C的官方建议是:当且仅当form是幂等(idempotent)的时候,使用get。幂等是一个数学上的术语,其定义是:对于一个函数f : D -> D,如果D中的所有x满足f (f x) = f x,那么这个函数是幂等的。HTTP specification(比如RFC 2616)中,将幂等解释为:多次相同请求产生的副作用,和一次请求的副作用相同。

打个比方,如果你提交一个form会从Google上查询一个关键词,那么我们可以认为这个form是幂等的,因为1次提交和10次提交的副作用是差不多的(10次查询可能会多消耗一些电能);如果你提交一个form是订购一个终极大黄蜂(Utimate bumblebee),那么这就不是幂等的:要是你不小心多提交了1次form的话,你可能会被老婆乱骂,你不小心又提交了10次的话,你可能就破产了——一次提交和多次提交的副作用明显不同,所以这不是幂等的。

所以,一般来说,如果提交这个请求纯粹只是从服务端获取数据而不进行其他操作,并且多次提交不会有明显的副作用,应该使用get。比如:

* 搜索引擎的查询:http://www.google.com/search?q=yandixin;
* 分页:ArticleList.asp?Page=1。

如果提交这个请求会产生其它操作和影响,就应该使用post。比如:

* 修改服务器上数据库中的数据;
* 发送一封邮件;
* 删除一个文件。

另一个要考虑的因素是安全性。见2.1。
5. 浏览器差异

* IE 6:URL长度限制为2083个字符;post之后,刷新页面不会自动重新post数据,会出现警告;

并且,在后退的过程中有可能出现“Page has Expired”(通常是向自己post,然后后退时):

微软的技术支持人员号称“this is not a bug or problem specified to the ASP.NET but a security feature of the IE Browser”,并且说“You can also inform your users of this”,实在是荒唐。另外,一篇KB也提到这个问题,说将Response.CacheControl设为”Public”即可,经测试仅在第一次后退时有效。
* IE 7:和IE 6相同;
* Firefox 2.0.0.11:刷新页面不会自动重新post数据,会出现警告;
* Opera 9.24:正常(自动post数据);
* Safari 3.0.4:post之后,刷新页面、前进、后退都不会自动重新post数据,会出现警告。

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628746.htmlTechArticleThis article will introduce you to an introductory tutorial about get and post examples of PHP form submission data. If you need to know Students can enter for reference. 1. What is a form? The so-called form is simple...
source:php.cn
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