HTML CSS JAVASCRIPT PYTHON JAVA

HTML Forms

HTML forms are used to collect information from users, such as names, email addresses, passwords, and messages. They are commonly used for registration pages, login screens, contact forms, surveys, and search boxes.

The HTML <form> element acts as a container for form controls. Elements such as <input>, <label>, <textarea>, <select>, and <button> allow users to enter information and interact with a webpage.

Basic HTML Form

A basic HTML form can contain text fields and a submit button. The <label> element describes each field, while the <input> element allows users to enter information.

Example
Try this code

In this example, the form contains two input fields and a submit button. The type="text" attribute creates a standard text field, while type="email" provides an input field designed for email addresses.

The for attribute in each label matches the id of its corresponding input. This connection makes the form easier to understand and use.

Note: This example demonstrates the form's appearance and basic browser behavior. It does not send information to a server because no form-processing destination has been configured.

The HTML Form Element

The <form> element groups related input controls into one form. It can use attributes such as action and method to specify where and how the browser submits the collected data.

Common HTML Input Types

The <input> element supports different types of user input. The type attribute determines how the field behaves.

Example
Try this code

The example demonstrates five useful input types:

Choosing an appropriate input type helps users enter information in the expected format.

The HTML Label Element

The <label> element provides a descriptive name for an input field. It helps users understand what information they should enter. For best results, connect each label to its input by matching the label's for attribute with the input's id.

The HTML Textarea Element

The <textarea> element creates a multiline text field. It is useful when users need to write longer messages, comments, feedback, or descriptions.

Example


Try this code

In this example, the <textarea> element allows users to write a message across multiple lines. The rows attribute controls the number of visible text rows, while cols sets an approximate character width. The placeholder attribute displays a short hint until the user enters text.

HTML Radio Buttons and Checkboxes

Radio buttons and checkboxes allow users to choose from predefined options.

Radio buttons that belong to the same group should share the same name attribute.

Example
Try this code

In this example, the user can select one learning level and multiple interests. Both radio buttons share the name level, so the browser treats them as one group. The checkboxes allow more than one interest to be selected.

The HTML Select Element

The <select> element creates a dropdown list. Each available choice is defined using an <option> element. Dropdowns are useful when users need to choose an item from a list.

Example
Try this code

The dropdown contains four course options. The value attribute specifies the value associated with each choice when the form is submitted. The visible option text helps users identify the course they want.

The HTML Submit Button

A submit button allows users to submit the data entered into a form. It is commonly created using <button type="submit"> or <input type="submit">.

However, displaying a submit button does not automatically store information or send it to a database. A working submission process requires an appropriate destination or application logic to receive and handle the data.

HTML Form Validation

HTML includes built-in validation features that help users provide the expected information before submitting a form. For example:

These browser checks improve the user experience, but they do not replace server-side validation when processing real submissions.

Example
Try this code

In this example, the name and email fields are required. The browser asks the user to complete these fields before allowing submission. The age field accepts numbers within the specified range of 13 to 100.

Best Practices for HTML Forms

Follow these practices when building forms:

Summary

HTML forms provide a standard way to collect information from users. The <form> element groups controls, <input> creates different types of fields, <label> identifies those fields, <textarea> accepts longer messages, and <select> provides dropdown choices. Submit buttons initiate the submission process, while validation attributes help check input in the browser.

By combining these elements correctly, you can build useful forms for contact pages, registrations, surveys, and other interactive website features.