Html 单击提交按钮时会发生什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21814294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:56:37  来源:igfitidea点击:

What happens when submit button is clicked

htmlget

提问by St.Antario

What happens when submit button is clicked? Let I've a form which located on an http://example.com/URL with the two inputelements like this:

单击提交按钮时会发生什么?让我有一个位于http://example.com/URL上的表单,其中包含如下两个input元素:

<form method="get">
    <input type="text" id="field1" name="namefield1"/>
    <input type="text" id="field2" name="namefield2"/>
    <input type="submit" value="submit"/>
</form>

What actually get request will be sent to an http-server in my specific case?

http在我的特定情况下,实际获取请求将发送到-server 吗?

采纳答案by Darin Dimitrov

The form will be submitted to the server and the browser will redirect away to the current address of the browser and append as query string parameters the values of the input fields.

表单将提交到服务器,浏览器将重定向到浏览器的当前地址,并将输入字段的值作为查询字符串参数附加。

In terms of the HTTP protocol the following GET request HTTP request will be sent:

就 HTTP 协议而言,将发送以下 GET 请求 HTTP 请求:

GET http://example.com/?namefield1=value1&namefield2=value2 HTTP/1.1
Host: example.com

Since your <form>is missing an actionattribute, the browser will simply redirect to the current url by appending the values as query string parameters. So if this form was loaded from http://example.com/foo.phpafter submitting it, the browser will redirect to http://example.com/foo.php?namefield1=value1&namefield2=value2where value1and value2will be the values enetered by the user in the corresponding input fields.

由于您<form>缺少一个action属性,浏览器将通过附加值作为查询字符串参数简单地重定向到当前 url。因此,如果这种形式是从加载http://example.com/foo.php在提交后,浏览器将重定向到http://example.com/foo.php?namefield1=value1&namefield2=value2其中value1value2将通过在相应的输入字段的用户enetered的值。

Also you might use your browser's built in debugging tools or Fiddlerto inspect the exact payload that gets sent to the server.

您也可以使用浏览器的内置调试工具或Fiddler检查发送到服务器的确切有效负载。

回答by B-Lat

If you submit the form with a method of 'get' then it will perform a get request thus sending the data held within your input elements on the query string as a name value pair. So for example http://example.com/index.html?field1=joe&field2=bloggs

如果您使用 'get' 方法提交表单,那么它将执行一个 get 请求,从而将您的输入元素中保存的数据作为名称值对发送到查询字符串上。例如http://example.com/index.html?field1=joe&field2=bloggs

See example here if you scroll down to the Submit Button example at the bottom: http://www.w3schools.com/html/html_forms.asp

如果您向下滚动到底部的提交按钮示例,请参阅此处的示例:http: //www.w3schools.com/html/html_forms.asp