button (type=submit) element

If you don't know what an element is or how you must use it, I recommend you read the "HTML tags and attributes" tutorial that you can find in the HTML tutorials section.

Description

The button element, having the "submit" value in its type attribute, represents a button that, when pressed, submits the form it belongs to. The label of a button is represented by the content of the element.

With the arrival of HTML5, several new attributes have been added to this element (formaction, formenctype, formmethod, formnovalidate and formtarget) that define and override certain parameters pertaining how the form must be submitted. These new attributes can be used, for example, to provide more than one submit button in one form, and make each of them perform a different type of submission.

In contrast with the input (type=submit) element, this type of button can contain other non-interactive elements.

Examples

In our first example we'll create a basic form with a couple of fields and a submit button. Here you'll be able to see and test the functionality of the submit button: when you press it, the form is automatically submitted.

<form action="../../form-result.php" target="_blank">
  <p>Username: <input type="text" name="username"></p>
  <p>Password: <input type="text" name="password"></p>
  <p><button type="submit">Submit form</button></p>
</form>

Username:

Password:

Now, we'll provide two submit buttons in the same form, and we'll make each perform a different type of submission. While the first button uses the default configuration declared in the form element, the second overrides some of the original attributes and produces a submission using the GET method, with no validation of data.

<form action="../../form-result.php" method="post" target="_blank">
  <p>Username: <input type="text" name="user" pattern="^[a-zA-Z][a-zA-Z0-9-_\.]{3,15}$" title="A proper username must begin with a letter, contain letters, numbers, scores and stops, and have between 3 and 15 characters long" required></p>
  <p>Password: <input type="password" name="pass" pattern="[a-zA-Z0-9]{6,15}" title="A valid password must be composed by letters and/or numbers and have a length between 6 and 15 characters" required></p>
  <p>
    <button type="submit">Send</button>
    <button type="submit" formmethod="get" formnovalidate>Send without validation</button>
  </p>
</form>

Username:

Password:

Our last example will show the special feature of the button element, which lets it hold other non-interactive elements. This is one of the main differences it has with the intput (type=submit) element.

<form action="../../form-result.php" target="_blank">
  <p>Search: <input type="search" name="searchstring"></p>
  <p><button type="submit">Submit <b>this</b> form<br>Ha! ha! ha!</button></p>
</form>

Search:

Attributes

Specific attributes

autofocus

A boolean value instructing the browser to set the focus to this control when the document has finished loading or when the dialog where the control finds itself is shown. If the attribute has the value "autofocus" or the empty string (""), or if it's just present, the control should get the focus as soon as possible, after the page or dialog has been loaded.

Example

<button type="submit" autofocus>Submit form</button>

disabled

A boolean value indicating wether the control is disabled or not. If the attribute takes the value "disabled" or the empty string (""), or if it's just present, the control will be disabled.

Disabled controls are rendered greyed out (if visible), are blocked from user interaction and, more importantly, their values (if any) aren't sent when the form is submitted.

Example

<form action="../../form-result.php" target="_blank">
  <p>
    Search for an item: <input type="search" name="searchitem">
    <button type="submit" disabled>Search</button>
  </p>
</form>

Search for an item:

form

The value of the id attribute of the form with which this control is associated to.

This attribute is new in HTML5 and helps defining the pertenence of controls in nested or distant forms.

Example

<form id="form1" action="../../form-result.php" target="_blank">
  <p>Names: <input type="text" name="names"></p>
</form>
<p><button type="submit" form="form1">Submit</button></p>

Names:

formaction

A URI indicating the location of the script responsible for the manipulation of the data sent by the form. This script is usually written in a server-side language like ASP, PHP, Python, etc.

This attribute is new in HTML5 and, if present, overrides the action attribute of the form element. This allows authors to provide one form with two or more buttons that perform different types of submissions.

Example

<form target="_blank">
  <p>
    Type: <input type="text" name="type">
    <button type="submit" formaction="../../form-result.php">Submit</button>
  </p>
</form>

Type:

formenctype

A value indicating the encoding method to be used for data when the form is submitted. There are three possible case-insensitive values:

  • application/x-www-form-urlencoded: spaces are replaced with plus signs ("+") and special characters are converted to HEX values. This is the default value.
  • multipart/form-data: no encoding is performed. This value is necessary for file uploads.
  • text/plain: only spaces are replaced by plus signs ("+").

Remember you must use the value "multipart/form-data" whenever a file is going to be uploaded in the form. Without this configuration, file uploads will fail.

This attribute is new in HTML5 and, if present, overrides the action attribute of the form element. This allows authors to provide one form with two or more buttons that perform different types of submissions.

Example

<form action="../../form-result.php" method="post" target="_blank">
  <p>Full name: <input type="text" name="fullname"></p>
  <p>Picture: <input type="file" name="picture"></p>
  <p>
    <button type="submit" formenctype="multipart/form-data">Send with picture</button>
    <button type="submit">Send without picture</button>
  </p>
</form>

Full name:

Picture:

formmethod

The method browsers should use to send the form's data. There are three possible case-insensitive values:

  • get: data is attached to the URL of the request (the one provided in the action attribute). The name-value pairs are arranged in the form "name=value" and separated each other with an ampersand sign ("&"). All this string is appended to the request URL preceded by a question mark ("?"). For example, a GET string could be: "form-result.php?user=john&pass=123456"
  • post: data is attached to the body of the request.
  • dialog: specific for forms inside a dialog element. Instructs the browser to close the dialog box upon form submission. Form results should be handled by scripts.

This attribute is new in HTML5 and, if present, overrides the action attribute of the form element. This allows authors to provide one form with two or more buttons that perform different types of submissions.

Example

<form action="../../form-result.php" target="_blank">
  <p>
    City: <input type="text" name="city">
    <button type="submit" formmethod="get">Send with GET</button>
    <button type="submit" formmethod="post">Send with POST</button>
  </p>
</form>

City:

formtarget

A value specifiyng where the results of the script in charge of processing the data should be loaded. This value can be a browsing-context name (like the value of the name attribute of an iframe) or any of the following values (case-insensitive):

  • _blank: in a new window/tab.
  • _parent: in the immediate parent context.
  • _self: in the same context that's containing the form.
  • _top: in the topmost context (the greatest parent context containing the form).

This attribute is new in HTML5 and, if present, overrides the action attribute of the form element. This allows authors to provide one form with two or more buttons that perform different types of submissions.

Example

<form action="../../form-result.php" method="post">
  <p>
    Group: <input type="text" name="group">
    <button type="submit" formtarget="_blank">Submit</button>
  </p>
</form>

Group:

formnovalidate

A boolean value instructing the browser not to validate the form's data upon submission. If this attribute takes the value "formnovalidate" or the empty string (""), or if it's just present, the browser should omit the form's validation during submission.

This attribute is new in HTML5 and, if present, overrides the action attribute of the form element. This allows authors to provide one form with two or more buttons that perform different types of submissions.

This attribute is part of the Constraint Validation API, a new feature that allows authors to provide form validation with minimal programming intervention.

Example

<form action="../../form-result.php" target="_blank">
  <p>
    Username: <input type="text" name="user" pattern="^[a-zA-Z][a-zA-Z0-9-_\.]{3,15}$" title="A proper username must begin with a letter, contain letters, numbers, scores and stops, and have between 3 and 15 characters long" required>
    <button type="submit">Send</button>
    <button type="submit" formnovalidate>Send without validating</button>
  </p>
</form>

Username:

name

A name for the control. This name will be sent by the browser to the processing agent, paired with the content of the value attribute. Both attributes together will conform a name-value pair that will be used to process the form data.

Currently, the value isindex, formerly used in a special way by some browsers and included in the HTML standard, isn't permitted in this attribute.

The name-value pair of a button is submitted, with the other form data, only if the button has been used to submit the form.

Example

<form action="../../form-result.php" target="_blank">
  <p>
    Username: <input type="text" name="username">
    <button type="submit" name="coolsubmitbutton" value="pressed">Submit</button>
  </p>
</form>

Username:

type

A value indicating the expected behavior of the button. There are four possible values (case-insensitive) that will decide the default action carried out by the button when it's pressed:

  • button: there's no default action associated to the control. The behavior of this type of buttons is usually provided by a script.
  • reset: the controls of the associated form are reset to their initial values.
  • submit: the associated form is submitted. This is the deafult value.
  • menu: the context menu associated to this button is deployed.

When this attribute isn't present, the button behaves as a "submit" button.

Example

<button type="submit">Submit data</button>

value

A value for the control. This value will be sent by the browser to the processing agent, paired with the content of the name attribute. Both attributes together will conform a name-value pair that will be used to process the form data.

The name-value pair of a button is submitted, with the other form data, only if the button has been used to submit the form.

Example

<form action="../../form-result.php" target="_blank">
  <p>
    Model: <input type="text" name="model">
    <button type="submit" name="coolsubmitbutton" value="pressed">Submit</button>
  </p>
</form>

Model:

Global attributes

For information about global attributes refer to this list of global attributes in HTML5.

Events

Global events

For information about global events refer to this list of global events in HTML5.