HTML - Forms Explanation with Examples Part-1

ALEKHYA

New member
HTML forms are a crucial component of web development that allow users to input data and interact with web pages. They are used to collect data from users, such as text, numbers, selections, and more, which can then be submitted to a server for processing. Forms typically consist of various types of input elements, such as text fields, checkboxes, radio buttons, dropdown menus, and buttons.

Here's a basic example of an HTML form:

<form action="/submit_form" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username">

<label for="password">Password:</label>
<input type="password" id="password" name="password">

<label for="email">Email:</label>
<input type="email" id="email" name="email">

<label for="age">Age:</label>
<input type="number" id="age" name="age">

<input type="submit" value="Submit">
</form>
```

In this example:

<form>: This tag defines the start of the form. It can have attributes such as `action` (specifies where to send the form data) and `method` (specifies the HTTP method to be used when submitting the form).
<label>: This tag provides a label for an input element. It's associated with the input via the `for` attribute.
<input>: This tag creates an input element. Its `type` attribute defines the type of input, such as text, password, email, number, etc. The `id` attribute uniquely identifies the input, and the `name` attribute specifies the name of the input which is used when submitting the form.
<br>: This tag creates a line break, used here for visual spacing.
<input type="submit">: This creates a submit button that, when clicked, submits the form data to the server specified in the `action` attribute.

HTML forms can be styled using CSS and can be manipulated with JavaScript to enhance interactivity. When a form is submitted, the data is typically sent to a server-side script for processing, which could be written in languages like PHP, Python, or JavaScript.

Form Attributes​

HTML forms can have various attributes that control their behavior and appearance. Here are some commonly used attributes:

1. action: Specifies the URL where the form data will be submitted when the form is submitted.
<form action="/submit_form" method="post">
2. method: Specifies the HTTP method to be used when submitting the form. Can be "get" or "post".
<form action="/submit_form" method="post">
3. name: Provides a name for the form, which can be used to reference the form in scripts.
<form name="myForm">
4. target: Specifies where to display the response that is received after submitting the form. It can be "_blank" to open in a new window or frame, "_self" to open in the same window/frame, "_parent" to open in the parent frame, or "_top" to open in the full body of the window.
<form action="/submit_form" method="post" target="_blank">
5. enctype: Specifies how form data should be encoded before sending it to the server. Common values include "application/x-www-form-urlencoded", "multipart/form-data", and "text/plain".
<form action="/submit_form" method="post" enctype="multipart/form-data">
6. autocomplete: Specifies whether the browser should automatically complete form fields based on earlier user input.
<form action="/submit_form" method="post" autocomplete="on">
7. novalidate: Specifies that the form should not be validated when submitted. Useful when you want to perform custom validation using JavaScript.
<form action="/submit_form" method="post" novalidate>
8. accept-charset: Specifies the character encoding used when the form is submitted to the server.
<form action="/submit_form" method="post" accept-charset="UTF-8">
These are just a few of the attributes that can be used with HTML forms. Depending on the specific requirements of your form, you may need to use additional attributes or JavaScript to achieve the desired functionality.

HTML Form Controls​

HTML forms can contain various types of controls or input elements that allow users to enter and submit data. Here are some common HTML form controls:

1. Text Input: Allows users to input single-line text.
<input type="text" name="username">
2. Password Input: Similar to text input, but the characters are masked (usually with asterisks) for security.
<input type="password" name="password">
3. Textarea: Allows users to input multiple lines of text.
<textarea name="message"></textarea>
4. Radio Buttons: Allows users to select one option from a group of options.
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
5. Checkboxes: Allows users to select one or more options from a group of options.
<input type="checkbox" name="interest" value="sports"> Sports
<input type="checkbox" name="interest" value="music"> Music
6. Select Dropdown: Presents a dropdown menu with selectable options.
<select name="country">
<option value="usa">USA</option>
<option value="uk">UK</option>
<option value="canada">Canada</option>
</select>
7. File Input: Allows users to select files to upload.
<input type="file" name="file">
8. Submit Button: Submits the form data to the server.
<input type="submit" value="Submit">
9. Reset Button: Resets all form controls to their default values.
<input type="reset" value="Reset">
10. Hidden Input: Allows you to include data that is not displayed to the user but is submitted with the form.
<input type="hidden" name="user_id" value="123">

These are some of the most commonly used form controls in HTML. They can be combined and customized to create forms for various purposes, such as user registration, data submission, and more. Additionally, HTML5 introduces new input types and attributes for form controls, offering more functionality and validation options.

Text Input Controls​

Text input controls in HTML allow users to enter single-line text data. Here are some commonly used text input controls:

1. Text Input: The basic text input control.
<input type="text" name="username">
2. Password Input: Similar to text input, but the characters are masked for security.
<input type="password" name="password">
3. Email Input: Specifically for entering email addresses, with built-in validation.
<input type="email" name="email">
4. Number Input: Allows users to input numeric values.
<input type="number" name="quantity" min="0" max="100">
5. Telephone Input: For entering telephone numbers.
<input type="tel" name="phone_number">
6. URL Input: Specifically for entering URLs, with built-in validation.
<input type="url" name="website_url">
7. Search Input: Styled as a search box and may have browser-specific behavior.
<input type="search" name="search_query">
8. Date Input: Allows users to input dates.
<input type="date" name="birthdate">
9. Time Input: Allows users to input times.
<input type="time" name="appointment_time">
10. Datetime Input: Allows users to input both date and time.
<input type="datetime-local" name="meeting_datetime">

These are some of the text input controls available in HTML. They provide different functionalities and validations depending on the type of data you want to collect. By using the appropriate input type, you can ensure better user experience and data integrity in your forms.

Single-line text input controls​

Single-line text input controls in HTML are typically used for capturing short text entries from users. Here are the commonly used single-line text input controls:

1. Text Input:
<input type="text" name="username">
This creates a simple text input field where users can enter single-line text.

2. Password Input:
<input type="password" name="password">
Similar to a text input, but the characters entered are masked (usually with asterisks) to enhance security.

3. Email Input:
<input type="email" name="email">
Specifically designed for entering email addresses. It often includes built-in validation to ensure that the entered text conforms to email format standards.

4. Number Input:
<input type="number" name="quantity" min="0" max="100">
Allows users to input numeric values. Depending on the browser, it may include controls such as spinners for easier numeric input.

5. Telephone Input:
<input type="tel" name="phone_number">
Designed for entering telephone numbers. It may provide specialized keyboards on mobile devices for easier number entry.

6. URL Input:
<input type="url" name="website_url">
Intended for entering URLs. It often includes built-in validation to ensure that the entered text is a valid URL.

7. Search Input:
<input type="search" name="search_query">
Styled as a search box, this input type may include browser-specific features such as autocomplete suggestions.

These single-line text input controls are versatile and can be used in various contexts to collect different types of data from users. They can be further customized using HTML attributes and CSS to match the design and functionality requirements of your web application.

 
Top