了解html表单提交

时间:2015-01-06 10:27:39

标签: html

想象一下,我有一个简单的形式:

<form id="add" method="POST" action="/add/emp">
    <label for="name">Name: </label>
    <input type="text" id="name"> <br>
    <input type="submit" value="Save">
</form>

我希望点击submit按钮后会发送一个请求http://localhost:8080/add/emp?name=_inputed_value_,但只有http://localhost:8080/add/emp没有任何参数。这是正常的行为,我需要用javaScript添加请求参数吗?

4 个答案:

答案 0 :(得分:2)

您正在使用表单方法POST。使用GET会产生所需的行为。

What is the difference between POST and GET?

答案 1 :(得分:2)

是的。 当您使用GET方法时,值会反映在您的URL中。您可以看到提交的值。但是当您使用POST方法时,这是不可能的。虽然表单已提交,但您无法在URL中看到值

答案 2 :(得分:1)

您的代码中的表单提交的POST方法不会通过URL参数对表单数据进行汇总。这种方法(POST)更安全,能够传输更大的数据!

要在浏览器网址中提交(并查看)您的表单数据,请使用method=GET

要在您的操作文件中获取表单数据:

$var = $_GET['form_field_name']; // if the method is GET
$var = $_POST['form_field_name']; // if the method is POST

答案 3 :(得分:0)

GET与POST

Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

$_GET is an array of variables passed to the current script via the URL parameters.

$_POST is an array of variables passed to the current script via the HTTP POST method.

何时使用GET?

Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

GET may be used for sending non-sensitive data.

Note: GET should NEVER be used for sending passwords or other sensitive information!

何时使用POST?

  

使用POST方法从表单发送的信息是不可见的   其他(所有名称/值都嵌入在HTTP的主体中)   请求)并且对要发送的信息量没有限制。

Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.