HTML Web Forms Tutorial For Coding Beginners

Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more

Web forms are used by virtually all websites for a wide range of purposes. Users of forums and social networks use forms to add content and interact with other users. Websites that can be customized to create a personalized experience, such as customizable newsfeeds, use forms to allow users to control the content that appears on the page. And nearly every website uses forms to allow website visitors to contact the organization or person administering the website. Web forms are made possible by the integration of multiple technologies:

  • HTML to create the form fields and labels and accept user input.
  • CSS to style the presentation of the form.
  • JavaScript to validate form input and provide Ajax-enabled interactions.
  • Server-side languages such as PHP to process form data.

In this guide, we’re going to cover all of the HTML elements used to create web forms. We also have other tutorials that cover topics such as building a form, styling and designing forms, and ensuring form usability and accessibility.

Defining the Structure of a Form

Every web form must be wrapped in <form> tags. In most cases, all of the form fields will be nested between these tags. There are several attributes that may optional be used with the form element, including: accept-charset: This optional attribute is used to identify the character encodings acceptable to the server and code processing form input. If more than one encoding is specified, one space should be placed between each encoding. If left blank or not provided, the encoding will default to the same encoding as the document containing the form. action: This attribute is used to specify a URL where form data should be sent (for instance: http://example.com/form_file.php). This field was required prior to HTML5 but is now optional. autocomplete: Use this attribute if you want the visitors browser to suggest form responses based on saved entries. The default value is autocomplete="on". If you turn autocomplete off you must also turn it off on every field that may allow autocompletion. enctype: Used to specify how form data should be encoded. Only used if the method attribute mentioned below is set to post. There are three possible values:

  • application/x-www-form-urlencoded: The default value which replaces all spaces with “+” and converts all special characters to ASCII HEX values.
  • multipart/form-data: Nothing is encoded. Input is uploaded to the server exactly as it is entered into the form.
  • text/plain: Spaces are converted into “+” symbols, but not other characters are encoded.

method: Dictates the HTTP method a website visitors browser should use to submit form data. If post is specified, form data is enclosed in the body of the HTTP request. If get is specified, form data is appended to the end of the URL specified in the action attribute with a “?” symbol, data length is limited to 300 characters, and form input is visible and can be bookmarked. name: Similar to an id attribute, a name is a unique identifier that may only be used once within an HTML document and may be used to select the form with JavaScript or another scripting language. novalidate: Used to override the default validation of form data. target: Specifies where to display the response received after submitting the form.

  • _self loads the response in the same frame.
  • _blank opens a new window or tab.
  • _parent is used when a form is nested in a descendant browsing context to load the response in the parent context and behaves the same as _self is there is no parent context.
  • _top is similar to _parent but select the top level browsing context rather than the immediate parent context.

Grouping Form Fields

The <fieldset> element is used to group together related form fields. It may also be used to contain an entire form to provide a visual cue that distinguishes the form from surrounding content. The first element within a fieldset is usually a <legend>. The legend will be displayed a part of the fieldset border giving website visitors a clue about the purpose of a form. For example, if a contact form were nested within a web page full of otherwise unrelated content, the fieldset element could be used in combination with the legend element to isolate the contact form from the rest of the content both semantically and visually.

<p>Paragraph content.</p> <form>   <fieldset disabled>     <legend>Contact Form</legend>     <!--These elements are discussed later in this tutorial-->     <p>Name: <input type="text" size="30"></p>     <p>Email: <input type="text" size="30"></p>     <p>Subject: <input type="text" size="30"></p>     <p>Message: <textarea>Type your message here</textarea></p>     <p><input type="submit"></p>   </fieldset> </form>  <p>Additional paragraph content</p> 

When rendered in the browser, the contact form would be clearly separated from surrounding content.

Paragraph content.

Contact Form

Name:
Email:
Subject:
Message:

Additional paragraph content

The fieldset element is optional but is commonly used to group related elements on long and complex forms, or to isolate form fields from nearby elements when a form is presented along with non-form content. HTML5 added three attributes which can be applied to fieldset elements:

  • disabled: Used to disable all of the forms in a fieldset. Note that we used this attribute in the example form above.
  • form: Used to associate a fieldset with the id of one or more form elements. Note that browser support for this attribute is very limited.
  • name: Associates a name with the fieldset.

The Input Element

Depending on the type of form you are creating, it’s entirely possible to have a form that only includes two types of elements: one form element and one or more input elements. The <input> element is used to create a variety of different types of input fields for form users to interact with.

Common <input type=""> Values