58

Here it says the action attribute is required on form elements:

http://www.w3schools.com/tags/att_form_action.asp

but I see that forms get submitted even if I don't specify an action attribute, and the form gets submitted to the current page which is exactly what I want.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Alex
  • 66,732
  • 177
  • 439
  • 641
  • 58
    Note: Don't rely on w3schools at all. They are not a reliable source. – kapa Jul 13 '12 at 12:13
  • 6
    However in this case w3schools is correct on both the HTML 4 and HTML 5 specs. In HTML 4, they list it as required (although most browsers still work without it) and in html 5 they list is as no longer required: http://www.w3schools.com/html5/att_form_action.asp – swannee Jul 13 '12 at 14:56
  • If its relevant, you can make your form submit happen on an iframe: http://stackoverflow.com/a/26380651/1695680 Thus not breaking your form's input behavior and also not reloading the page. – ThorSummoner Mar 17 '15 at 17:53
  • 2
    I always use the Mozilla reference docs (The people who make Firefox), https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form Its as easy as prefixing 'mdn' in your searches. Though be careful as 'msdn' will bring you to the Microsoft Docs which are usually just as bad as w3's. – ThorSummoner Mar 17 '15 at 17:58

4 Answers4

62

The requirement is only by standards. It is perfectly possible to do whatever you want on a page and not follow standards. Things may not display or work correctly if you do that, but likely they will. The goal is to follow them, and the idea is that if you follow them, your page will always work; you don't have to worry about anything.

Yes, the form is required to have an action attribute in HTML4. If it's not set, the browser will likely use the same method as providing an empty string to it. You really should set action="" which is perfectly valid HTML4, follows standards, and achieves the same exact result.

In HTML5, you can actually specify an action on the submit button itself. If there isn't one, it uses the form's action and if that is not set, it defaults to the empty string (note you cannot explicitly set the action to an empty string in HTML5).

animuson
  • 53,861
  • 28
  • 137
  • 147
  • 4
    One other note, if you choose to leave out the action attribute, I'm pretty sure it defaults to submit to the same page. – clamchoda Feb 22 '12 at 19:30
  • @Chris: I said it likely does. I can't vouch for all past browsers because honestly I don't know what older browsers did. If it's not set, it's really up to the browser, but I believe they will all default to that action. – animuson Feb 22 '12 at 19:31
17

It looks like the HTML4 spec requires it. I suspect some browsers do what you want to "make things easier". I don't recommend relying it on though. Since you're in undefined behavior, a browser could reasonably decide to just do nothing when the form is submitted with no action.

You can get the behavior you want while following the spec by leaving the action blank (since it's relative, blank means the current page):

<form action="" ...>

As mentioned by bazmegakapa, the HTML5 spec doesn't seem to require the action attribute:

The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces.[emphasis added]

Interestingly, this means in HTML5, <form action=""> is not valid, but it's not clear if a form without an action is required to work (submit to the current page).

rybo111
  • 12,240
  • 4
  • 61
  • 70
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
7

In HTML 5 they list the action attribute as no longer required: http://www.w3schools.com/html5/att_form_action.asp which is in accordance with the HTML 5 specs.

Technically it's a violation of the HTML 4 spec, but all browsers will post back to the originator of the response if no action is specified. I would agree it's not a smart idea to rely on it but it does work.

TylerH
  • 20,799
  • 66
  • 75
  • 101
swannee
  • 3,346
  • 2
  • 24
  • 40
  • 5
    There is nothing called w3schools HTML spec. w3schools has nothing to do with w3c, and the HTML spec is released and maintained by w3c. w3schools is not official at all, it's just a site with a bunch of half-technical information and good SEO. The HTML5 spec (which the question asks about) is surely not violated by doing what the OP asks. Do you always attack people who try to correct you? I don't find your behaviour professional. – kapa Jul 13 '12 at 14:04
4

If after all button formaction and form action attributes have been assessed, if "action" still evaluates as "empty string", then from HTML5.2 spec section 4.10.21.3 point 8 states:

If action is the empty string, let action be the document’s URL of the form document.

when it comes to submission of the form, which is what you wanted.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Neil Moss
  • 6,598
  • 2
  • 26
  • 42
  • This some kind of a paradox. Literally here it says it can be empty and [elsewhere](https://www.w3.org/TR/html5/sec-forms.html#element-attrdef-form-action) it says that `action` must be a non-empty URL. –  Apr 06 '19 at 10:39
  • @RedClover be aware those are NOT the same standards :) let's say the `w3.org` is the really hard core official one, WHATWG is the real life living standard they go together a lot of times, but may differ in edge cases where WHATWG tends to be much more benevolent because of backwards compatibility :) It's like the infamous `
    ` tag - officially not supported by browsers, but it still works x)
    – jave.web Jun 29 '20 at 17:18