top of page

Forms Basics: Form tag, action and method attributes, Form Elements: Text inputs, Password inputs, Radio buttons, Checkboxes, Submit and reset buttons, Text area, Select dropdown, Labels tag in html

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Feb 27, 2025
  • 4 min read

Forms Basics in HTML

Forms are an essential part of web development, allowing users to input data that can be sent to a server for processing. HTML provides a variety of elements to create forms, including the <form> tag, various input types, and other elements like <textarea> and <select>.

1. <form> Tag

The <form> tag is used to create an HTML form. It acts as a container for all the form elements. The two most important attributes of the <form> tag are:

  • action: Specifies the URL where the form data will be sent when the form is submitted.

  • method: Defines the HTTP method used to send the form data. The two most common methods are:

    • GET: Appends form data to the URL (visible in the address bar).

    • POST: Sends form data in the HTTP request body (not visible in the address bar).

Example:

html

<form action="/submit-formmethod="POST">
  <!-- Form elements go here -->
</form>

2. Form Elements

a) Text Inputs

Text inputs allow users to enter a single line of text. Use the <input> tag with type="text".

Example:

html

<label for="username">Username:</label>
<input type="textid="usernamename="username">

b) Password Inputs

Password inputs are similar to text inputs but hide the entered text for security. Use the <input> tag with type="password".

Example:

html

<label for="password">Password:</label>
<input type="passwordid="passwordname="password">

c) Radio Buttons

Radio buttons allow users to select one option from a group. Use the <input> tag with type="radio". All radio buttons in the same group must have the same name attribute.

Example:

html

<label for="male">Male</label>
<input type="radioid="malename="gendervalue="male">

<label for="female">Female</label>
<input type="radioid="femalename="gendervalue="female">

d) Checkboxes

Checkboxes allow users to select multiple options. Use the <input> tag with type="checkbox".

Example:

html

<label for="subscribe">Subscribe to newsletter</label>
<input type="checkboxid="subscribename="subscribevalue="yes">

e) Submit and Reset Buttons

  • Submit Button: Submits the form data to the server. Use the <input> tag with type="submit".

  • Reset Button: Resets all form fields to their default values. Use the <input> tag with type="reset".

Example:

html

<input type="submitvalue="Submit">
<input type="resetvalue="Reset">

f) Text Area

The <textarea> element allows users to enter multiple lines of text. It has rows and cols attributes to define its size.

Example:

html

<label for="comments">Comments:</label>
<textarea id="commentsname="commentsrows="4cols="50"></textarea>

g) Select Dropdown

The <select> element creates a dropdown list. Use the <option> tag to define the available options.

Example:

html

<label for="country">Country:</label>
<select id="countryname="country">
  <option value="usa">United States</option>
  <option value="canada">Canada</option>
  <option value="uk">United Kingdom</option>
</select>

h) Labels Tag

The <label> tag is used to define a label for a form element. It improves accessibility by associating the label with the input element using the for attribute (which matches the id of the input).

Example:

html

<label for="email">Email:</label>
<input type="emailid="emailname="email">

Complete Example of a Form

html

<form action="/submit-formmethod="POST">
  <!-- Text Input -->
  <label for="username">Username:</label>
  <input type="textid="usernamename="username"><br><br>

  <!-- Password Input -->
  <label for="password">Password:</label>
  <input type="passwordid="passwordname="password"><br><br>

  <!-- Radio Buttons -->
  <label>Gender:</label>
  <input type="radioid="malename="gendervalue="male">
  <label for="male">Male</label>
  <input type="radioid="femalename="gendervalue="female">
  <label for="female">Female</label><br><br>

  <!-- Checkbox -->
  <input type="checkboxid="subscribename="subscribevalue="yes">
  <label for="subscribe">Subscribe to newsletter</label><br><br>

  <!-- Text Area -->
  <label for="comments">Comments:</label><br>
  <textarea id="commentsname="commentsrows="4cols="50"></textarea><br><br>

  <!-- Select Dropdown -->
  <label for="country">Country:</label>
  <select id="countryname="country">
    <option value="usa">United States</option>
    <option value="canada">Canada</option>
    <option value="uk">United Kingdom</option>
  </select><br><br>

  <!-- Submit and Reset Buttons -->
  <input type="submitvalue="Submit">
  <input type="resetvalue="Reset">
</form>

Key Points to Remember:

  1. Always use the <label> tag to improve accessibility.

  2. Use the name attribute to identify form data when it is submitted.

  3. Choose the appropriate method (GET or POST) based on the sensitivity of the data.

  4. Use the action attribute to specify where the form data should be sent.

 
 
 

Comments


bottom of page