<form> tag

HTML <form> tag

Form helps to collect information about user who visits your website.

For example – Login form where user can provide their personal details like Name, Email id, Phone number and other contact information. 

You can add fields like Radio buttons, Submit buttons, checkboxes, textarea, etc in the form. 

Click Here to learn about HTML Forms with multiple examples.

It is always better to style the HTML forms. To style forms, master CSS Forms.

Syntax:
<form action=”URL” method=”postname.php”> … </form>

Example

<form> 
  First Name: <input type="text" name="firstname"/>
  <br/> 
  <br/>
  Last Name: <input type="text" name="lastname"/> 
</form>

Default value

 display: block;

How Browser will display

BLOCK Level.

Difference between HTML4.01, XHTML and HTML5

HTML4 XHTML HTML5
NA XHTML uses id attribute instead of name attribute HTML5 contains new attributes like autocomplete, novalidate etc. Here, the accept attribute is deprecated.

Attributes used with <form> tag

Attribute Description
target This decides where the user will see the response or message once he/she submits the form. It can take any one ot these values -

  • _blank - This opens the response in a new window/tab

  • _self- The shows the response in the same window/session

  • _parent - Opens the response in the parent tab

  • _top - Display the response in the full screen
  • accept-charset Defines the list of Character set (Character encodings) that the server accepts for processing.
    action Once the user provides the information in the HTML form, you need to send this data to server to process them. The 'action' attribute sets the path/URL which will be a separate page which will process the information.
    autocomplete Sets whether the form will have the autocomplete on or off. It is added in HTML5. It can take the values as either on or off.
    enctype Defines how to encode the form data before sending it to the server. The method="post" uses this. It can accept these values -

  • application/x-www-form-urlencoded
  • multipart/form-data
  • text/plain
  • formaction Once the user submits the form, this attribute decides where to send the form data using a URL/path. This path is linked to another page which will process the user data.
    method It decides whether to use the HTTP get method or the HTTP post method while sending the data from the form.
    name When you code the 'name' attribute on the form, it sets the name of the form. When you code this attribute on an element, then it sets the name of that particular element.
    novalidate It tells the browser not to validate the form when the user sbmits it. This is a boolean value which can be eithter true or false. It is added in HTML5.
    accept It contains comma seperated list of file types and the server accepts this file
    (HTML5 does not support this attribute).

    Global Attributes

    The <form> tag supports Global attributes.

    Event Attributes

    The <form> tag supports Event attributes.

    Supporting Browsers

    supporting-browsers for tags

    Related Tags

    <input>, <datalist>, <keygen>, <output>, <textarea>, <label>, <legend>, <fieldset><button>, <select> & <option> tags.

    Tutorials for all brains!