53

Here it says it's required

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.

kapa
  • 75,446
  • 20
  • 155
  • 173
Alex
  • 64,868
  • 164
  • 416
  • 621
  • 53
    Note: Don't rely on w3schools at all. They are not a reliable source. – kapa Jul 13 '12 at 12:13
  • 5
    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

61

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
  • 52,378
  • 28
  • 138
  • 145
  • 3
    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
  • I don't know how it was in 2013 when the answer was written, but currently in HTML5 action="" is valid... also without action attribute is ok – instead Mar 19 '14 at 22:06
  • 15
    @instead According to the [current HTML5 specification](http://www.w3.org/TR/html5/forms.html#attr-fs-action): *"The `action` and `formaction` content attributes, if specified, must have a value that is a valid **non-empty** URL potentially surrounded by spaces."* – animuson Mar 19 '14 at 22:29
  • Thanks for that, I was to lazy and checked my HTML code with w3c validator and it didn't gave me any warning – instead Mar 19 '14 at 22:33
  • Very useful info. Thanks – Sammu Sundar Jul 04 '19 at 10:42
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
  • 11,742
  • 4
  • 58
  • 66
Brendan Long
  • 51,248
  • 19
  • 139
  • 179
  • 2
    The question has an HTML5 tag, and this answer talks about HTML4. – kapa Jul 13 '12 at 12:15
  • 2
    @bažmegakapa Instead of voting down all of the answers, it would be more useful to post your own answer with a link to the HTML5 spec. I agree with swannee that the whole "downvote every answer" instead of just leaving a comment (or edit suggestion) is bad form. Our answers are both correct, just not as specific as you want (and we're both perfectly willing to improve our answers with more information). – Brendan Long Jul 13 '12 at 16:10
  • 1
    Downvoted because the answers were wrong and spreading misinformation. Removed my downvote, added upvote, thanks. – kapa Jul 13 '12 at 16:46
6

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.

EDIT: As it has been pointed out to me that this question is tagged as HTML 5: 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.

swannee
  • 3,206
  • 1
  • 20
  • 36
  • 3
    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
  • 1
    I don't want to risk that your head blows up, but the question had nothing to do with HTML4. That's all I wanted to say. I just don't want future visitors to find misinformation. I have no problem with you other than the fact that you are acting childish. [HTML5 spec 4.10.3 The form element](http://www.w3.org/TR/2011/WD-html5-20110525/forms.html#the-form-element) – kapa Jul 13 '12 at 14:10
  • 1
    How do you know? HTML 5 is in the tag, but never mentioned in the question, and as well just plain HTML is also in the tags.... – swannee Jul 13 '12 at 14:19
  • I added plain html myself a few hours ago. I will remove it then to avoid confusion. You can check the edit history. – kapa Jul 13 '12 at 14:21
  • 2
    Sorry, just think it's bad SO form to downvote reasonable answers that add to the conversation because of some technicality that's not explicitly stated in the question. In addition, it's also important because although it is against the HTML 4 spec, it does in fact work in all major browsers that support HTML 4. – swannee Jul 13 '12 at 14:29
  • I find a tag quite explicit. But no problem, I did not know this about HTML5 myself. I was looking for the answer, and found this question. Later, I read the HTML5 spec, then I came back here to prevent other people from finding misinformation. I find my downvote justified, but I will of course remove it if you make your answer technically correct, because I don't downvote for fun (even if I could have gotten upset at you calling me things, but I'm not that childish). – kapa Jul 13 '12 at 14:33
3

// thread resurrection alert

To extend upon animuson's answer...

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.

Neil Moss
  • 6,454
  • 2
  • 24
  • 40
  • 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