Home Videos Exercises MCQ Q&A Quiz E-Store Services Blog Sign in Appointment Payment

HTML Form

An HTML form is used to collect user input. The user input is most often sent to a server for processing.


What is HTML Form

An HTML form is a section of a web page that allows users to input data, which is then sent to a server for processing.

Forms can be used for a variety of purposes, such as collecting information for surveys or registration processes, or for entering shipping or credit card data to order a product.

HTML forms are made up of a container called the HTML form element, which contains other HTML form elements, such as: <input>, <label>, <select>, <textarea>, <button>, <fieldset>, <legend>, <datalist>, <output>, and <option>.


The <form> Element

The HTML <form> element is used to create an HTML form for user input:

Syntax: <form> ... form elements .. </form>

The <form> element is a container for different types of input elements, like: text fields, checkboxes, radio buttons, submit buttons, etc.


The <input> element

The HTML <input> element is the most used form element.

An <input> element can be displayed in many ways, depending on the type attribute.

Here are some examples:

; ;
Type Description
<input type="text"> Displays a single-line text input field
<input type="button"> Displays a clickable button
<input type="submit"> Displays a submit button (for submitting the form)
<input type="radio">Displays a radio button (for selecting one of many choices)
<input type="checkbox">Displays a checkbox (for selecting zero or more of many choices)


Here are the key points to consider when creating an HTML form:

1. Form Element (<form>):

  • Action: Specifies the URL where form data is sent.
  • Method: Defines how form data is sent. Common methods
    • GET: Data sent via URL parameters (visible in the URL).
    • POST: Data sent in the HTTP body (more secure).

2. Form Inputs (<input>):

  • Text Input: Collects plain text
  • Password Input: Masks input (e.g., for passwords)
  • Radio Buttons: Choose one option from a group
  • Checkboxes: Multiple selections are allowed
  • Submit Button: Submits the form

3. Labels (<label>):

  • Used to associate labels with form elements for better accessibility

4. Textarea (<textarea>):

  • Collects larger amounts of text input (e.g., comments or messages)

5. Select Dropdown (<select>):

  • Allows users to select one or more options from a dropdown.

6. File Upload (<input type="file">):

  • Allows users to upload files

7. Form Validation:

  • Required Fields: Add required to input fields
  • Pattern Matching: Use pattern to define a regular expression for input validation

8. Placeholders:

  • Provides hints for the expected input

9. Fieldset and Legend:

  • Group related elements within a form

10. Form Submission:

  • Use the submit button to trigger the form submissi