The HTML Form Element
FORM element is the element that surrounds all forms on a web page. HTML5 no longer requires form elements to be inside the
FORM element, unless there are multiple forms on the page.
FORM element has the following attributes:
action(required in HTML 4.01)
In HTML5 you can replace the
actionattribute with an event attribute like
onclickto activate the form. If you use an event attribute in HTML 4.01, you still need to have the
actionattribute set to a default value, like:
action="#"for the form to validate.
methodattribute defines how the browser should submit the form data. There are two methods you can use:
getmethod puts the information in a query string.
Getis the default method, and sends the form data attached to the URL after a question mark (
?). For example:
postmethod sends the form information as a data block to the server through HTTP protocols. This method is not visible to the end user, and is best for most standard forms.
enctypeattribute indicates the format of the data submitted to the server. This is most often used with forms where there is a file upload option. The default is
application/x-www-form-urlencoded. However, if you want to do file upload you must use the encoding type
multipart/form-datato ensure that your files will be transferred correctly.
accept(obsolete in HTML5)
This is a list of the MIME types that the server will accept.
This is a list of the character sets that the server will accept.
Plus there are some new and changed attributes in HTML5:
This attribute changed in HTML5 to be included in the
FORMelement as the name portion of a name/value pair used for form submissions.
novalidateattribute is new in HTML5 and indicates that the form should not be validated by the browser during submission. This will not affect scripts that you set on the form to validate the data, only on the browser itself doing validation.
In HTML5 you can now set the
targetattribute of a form to have any results page open in a different frame or window. This attribute works the same way you would target an iframe.
autocompleteattribute is new in HTML5. It specifies whether the browser should store form data and then complete form fields as the user types. Like the
novalidateattribute, this attribute does not affect autocomplete functions you have added with scripts, only what the browser would do by default.
This article is part of the HTML Forms Tutorial