Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is action really required on forms?

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.

like image 559
Alex Avatar asked Feb 22 '12 19:02

Alex


People also ask

Can a form have no action?

A form without an action attribute is not a form, according to standards - and will actually cause a page reload in some browsers..

Can I leave form action blank?

If you leave it out, the form will be submitted to the document's address, i.e. the same page. It is also possible to leave it empty, and any browser implementing HTML's form submission algorithm will treat it as equivalent to the document's address, which it does mainly because that's how browsers currently work: 8.

What is the use of action in form?

The action attribute defines the action to be performed when the form is submitted. Usually, the form data is sent to a file on the server when the user clicks on the submit button.

What is the purpose of post and get actions of a form?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.


2 Answers

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).

like image 57
animuson Avatar answered Sep 20 '22 01:09

animuson


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).

like image 26
Brendan Long Avatar answered Sep 20 '22 01:09

Brendan Long