Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Forms in HTML?

In this blog post, we will talk about one of the most essential elements of web development: forms in HTML. When you are learning programming, especially web development, you will inevitably come across forms. They are the cornerstone of user interaction on websites, allowing users to submit data, send messages, search for information, and much more.

What are forms?

Think of forms as a way for users to provide input to a website. They allow users to enter data, like their name, email, address, etc., that can be sent to the server for processing. For example, when you sign up for a newsletter or create an account on a website, the data you enter is collected through a form.

In HTML, a form is created using the <form> element, which can contain various types of input elements such as text boxes, dropdown lists, checkboxes, radio buttons, and more.

Basic structure of a form

A simple form in HTML consists of the following components:

  1. The <form> element: This is the container for all the input elements and buttons that make up the form.
  2. Input elements: These are the actual fields where users enter data, like <input>, <textarea>, <select>, etc.
  3. A submit button: This is a button that, when clicked, sends the data entered in the form to the server.

Here's an example of a basic form in HTML:

<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
  <br>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email">
  <br>
  <input type="submit" value="Submit">
</form>

In this example, we have a form with two input elements, one for entering a name and another for entering an email address. The form also includes a submit button. When the user clicks on the submit button, the data entered in the form is sent to the server.

Input elements

As mentioned earlier, there are various types of input elements that you can use in a form. Let's go through some of the most common ones.

Text input

A text input is a single line input field that allows users to enter plain text. It is created using the <input> element with the type attribute set to "text".

Example:

<label for="username">Username:</label>
<input type="text" id="username" name="username">

Textarea

A textarea is a multi-line input field that allows users to enter plain text. It is created using the <textarea> element.

Example:

<label for="message">Message:</label>
<textarea id="message" name="message"></textarea>

Checkbox

A checkbox is a type of input that allows users to select one or more options from a list. Checkboxes are created using the <input> element with the type attribute set to "checkbox".

Example:

<label for="subscribe">Subscribe to newsletter:</label>
<input type="checkbox" id="subscribe" name="subscribe">

Radio button

A radio button is a type of input that allows users to select only one option from a list. Radio buttons are created using the <input> element with the type attribute set to "radio".

Example:

<label for="gender_male">Male:</label>
<input type="radio" id="gender_male" name="gender" value="male">
<label for="gender_female">Female:</label>
<input type="radio" id="gender_female" name="gender" value="female">

A dropdown list, or a select box, is a type of input that allows users to select one option from a list. Dropdown lists are created using the <select> element, with <option> elements inside it representing the individual options.

Example:

<label for="country">Country:</label>
<select id="country" name="country">
  <option value="usa">United States</option>
  <option value="canada">Canada</option>
  <option value="mexico">Mexico</option>
</select>

Form submission

When a user fills out a form and clicks the submit button, the data entered in the form is sent to the server. To specify where the data should be sent, you need to set the action attribute on the <form> element. The action attribute should be set to the URL of the server-side script that will process the form data.

Additionally, you need to specify how the data should be sent to the server. This is done using the method attribute on the <form> element. The method attribute can be set to either "GET" or "POST", depending on the type of request you want to send to the server.

Example:

<form action="https://example.com/submit-form" method="post">
  <!-- Input elements go here -->
  <input type="submit" value="Submit">
</form>

In this example, when the user submits the form, the data will be sent to the URL "https://example.com/submit-form" using the POST method.

Conclusion

Forms are a crucial part of web development, allowing users to interact with websites and send data to servers. By learning how to create and use forms in HTML, you've taken a significant step towards becoming a proficient web developer. Keep experimenting with different input elements and form attributes to build more complex and interactive forms for your projects.