Can the action attribute on a form element be empt

2020-05-01 22:48发布

As of HTML5, the widely used and recommended action="" is invalid HTML.

From the specs:

The action and formaction content attributes, if specified, must have a value that is a valid URL.

So what is the correct way to have the action attribute point to the current page?

I am currently using

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

标签: php html forms
3条回答
我欲成王,谁敢阻挡
2楼-- · 2020-05-01 23:12

No, in HTML5, the action attribute may be omitted, and it defaults to the emoty string, which means a reference to the current document.

查看更多
Lonely孤独者°
3楼-- · 2020-05-01 23:15

It's true that the action= attribute may not be empty, as an empty string is not a valid URL. However, it is valid to simply leave it out, and let the browser figure it out.

The browser will assume the current page.

查看更多
贪生不怕死
4楼-- · 2020-05-01 23:26

You can set value to '.' or do not set action attribute at all.

<form method="post">
or
<form method="post" action=".">

(I was surprise but it is really invalid to leave it blank, at least validator.w3.org is returning error).

查看更多
登录 后发表回答