在自己的页面上提交 HTML 表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13520127/
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 04:01:20  来源:igfitidea点击:

Submit HTML form on self page

htmlformshttpurl

提问by Oto Shavadze

I want an HTML form to submit to itself. How do I use the actionattribute?

我想要一个 HTML 表单提交给自己。如何使用action属性?

  1. <form action="">
  2. <form action="#">
  3. <form action="some/address">
  4. <form>
  1. <form action="">
  2. <form action="#">
  3. <form action="some/address">
  4. <form>

Which was is preferable?

哪个更可取?

回答by Milche Patern

In 2013, with all the HTML5 stuff, you can just omit the 'action' attribute to self-submit a form

在 2013 年,对于所有 HTML5 内容,您可以省略 'action' 属性以自行提交表单

<form>

Actually, the Form Submission subsection of the current HTML5 draftdoes not allow action="" (empty attribute). It is against the specification.

实际上,当前 HTML5 草案表单提交小节不允许 action=""(空属性)。这是违反规范的。

From this other Stack Overflow answer.

这个其他堆栈溢出答案

回答by Le Codeur

Use ?:

使用?

<form action="?" method="post">

It will send the user back to the same page.

它会将用户发送回同一页面。

回答by muratgozel

You can leave action attribute blank. The form will automatically submit itself in the same page.

您可以将操作属性留空。表单将在同一页面中自动提交。

<form action="">

According to the w3c specification, action attribute must be non-empty valid url in general. There is also an explanation for some situations in which the action attribute may be left empty.

根据w3c 规范,action 属性一般必须是非空的有效 url。对于某些可能将 action 属性留空的情况,还有一个解释。

The action of an element is the value of the element's formaction attribute, if the element is a Submit Button and has such an attribute, or the value of its form owner's action attribute, if it has one, or else the empty string.

元素的 action 是元素的 formaction 属性的值,如果元素是提交按钮并具有这样的属性,或者其表单所有者的 action 属性的值(如果有),否则为空字符串。

So they both still valid and works:

所以它们仍然有效并且有效:

<form action="">
<form action="FULL_URL_STRING_OF_CURRENT_PAGE">

If you are sure your audience is using html5 browsers, you can even omit the action attribute:

如果您确定您的观众使用的是 html5 浏览器,您甚至可以省略 action 属性:

<form>

回答by Buts

If you are submitting a form using php be sure to use:

如果您使用 php 提交表单,请务必使用:

action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"

for security.

为了安全。

回答by DarkAjax

You can do it using the same page on the action attribute: action='<yourpage>'

您可以在 action 属性上使用相同的页面: action='<yourpage>'