How to Work with Forms in HTML5?

How to Work with Forms in HTML5? Working with forms in HTML entails designing a user interface for collecting and submitting data to a web server. Forms are an essential component of web development, used for a variety of reasons including user identification, data entry, and feedback posting. To collect user input in HTML forms, create a <form> element. Create form fields using input components like <input>, <textarea>, and <select>. The action attribute defines where the form data should be sent, whereas the method attribute specifies the HTTP method (GET or POST). Use labels (<label>) to define form fields and increase accessibility. Use CSS for styling and JavaScript for validation if needed.

Exploring the FORM Element:

HTML’s <form> element creates a form to collect user input. It has form controls such as input fields, buttons, checkboxes, and radio buttons. Here’s a detailed look at the <form> element:

1. Syntax: The <form> element has various characteristics that define its behavior and appearance:

  • action: Specifies the URL to which form data should be submitted.
  • method: Indicates which HTTP method to use when submitting the form (GET or POST).
  • The target specifies where the response should be displayed after submitting the form (_blank, _self, _parent, _top).
  • The enctype specifies how form data should be encoded before being sent to the server (application/x-www-form-urlencoded, multipart/form-data, and text/plain).

2. Form Controls: You can collect multiple input forms by including various form controls within the <form> element:

  • <input>: Accepts text, password, checkbox, radio, file upload, and other input types.
  • <textarea>: Enables multi-line text input.
  • <select> is for drop-down lists.
  • <button>: For buttons that submit or reset the form.
  • <label>: Link a label to a form control.

3. Labeling Form Controls: Use the <label> element to label form controls. This enhances both accessibility and usability.

4. Form Submission:

  • When the form is submitted, the browser transmits the form data to the URL given by the action property.
  • For GET requests, the form data is appended to the URL.
  • POST requests include the form data in the request body.

5. Validation:

  • HTML5 includes built-in form validation with properties such as needed, min, max, and pattern.
  • To create custom validation logic, use JavaScript.

6. Accessibility: To make your form accessible, use proper labeling (<label>), semantic HTML components, and ARIA attributes.

7. Styling: Style your form elements with CSS to fit the appearance of your website.

8. Security: To prevent data interception, ensure form input is submitted via HTTPS. Validate and sanitize form data on the server to prevent security flaws.

AttributeDescriptionNew in HTML5
actionrefers to the Uniform Resource Locator (URL) of the program in the server that processes the formno
autocompleteenables the autocomplete feature in a formyes
accept-characterrefers to the character set in the form that can be accepted by the serverno
enctypespecifies how the information in the form should be encoded before sending it to the serverno
methodspecifies how the information is sent from the browser to the serverno
namerefers to the name of the formno
novalidatespecifies that the form should not be validated while submittingyes
targetopens the action URL in the specified target, such as in the same window, in a new window, or in a new tabno
Table 1 – Attributes of the FORM Element
Exploring Types of the INPUT Element

In HTML, the <input> element can be used with multiple type properties to construct various types of form controls. Here’s a thorough list of the type properties and their respective form controls:

  • Text: Creates a single-line text input field.
  • Password: Creates a password input field where the text is masked.
  • Checkbox: Creates a checkbox for selecting multiple options.
  • Radio: Creates a radio button for selecting one option from a group.
  • Submit: Create a submit button to submit the form.
  • Reset: Create a reset button to reset the form fields.
  • File: Creates a file upload control to select files.
  • Hidden: Creates a hidden input field not shown to the user.
  • Number: Creates a numeric input field.
  • Date: Creates a date input field.
  • Email: Creates an email input field.
  • URL: Creates a URL input field.
  • Search: Creates a search input field.
  • Tel: Creates a telephone number input field.
  • Color: Creates a color picker input field.
  • Range: Creates a slider control for selecting a value.
  • Time: Creates a time input field.
  • Week: Creates a week input field.
  • Month: Creates a month input field.
  • Datetime: Creates a datetime input field.
  • Datetime-local: Creates a datetime-local input field.

Let’s start our discussion with the text and search type:

1. Describing the text and search Type: The <input> element in HTML can be used with the type property set to text or search to generate text input fields. Each serves a specific purpose:

  • Text Type (type=”text”): The text type generates a normal, single-line text input field. It is used for generic text input, allowing users to enter any text, including their name, address, or message.
  • Search Type (type=”search”): The search type generates a text input box exclusively for entering search queries. It frequently incorporates a built-in search icon or design to signify its purpose. It is commonly used in search bars or search forms to enable users to enter search phrases.
  • Behavioral Differences: The search type may feature a browser-specific style, such as a magnifying glass symbol within the input field, to visually identify its purpose. Some browsers may also offer additional functions for search inputs, such as displaying search history or providing autocomplete recommendations based on search terms.
  • Accessibility: When utilizing the search type, ensure that the input field’s purpose is evident to screen readers and other assistive technology. Using relevant labels (<label>) and ARIA characteristics can enhance accessibility.
  • Styling: Both text and search input boxes can be styled with CSS to match the website’s appearance. However, the search type may include a browser-specific style that distinguishes it from a conventional text input field, such as a default border or padding.

Overall, while both text and search input fields allow users to enter text, the search type is intended especially for submitting search queries and may have additional browser-specific appearance and behavior.

2. Describing the tel Type:

The tel type of the HTML <input> element creates a text input field suited for entering phone numbers. Using <input type=”tel”> indicates to the browser that the anticipated input is a phone number. This can prompt mobile devices to adopt specialized input techniques, such as a numeric keypad with phone number symbols (e.g., brackets, hyphens).

Here’s a brief description of the tel type:

  • The tel type is used to obtain phone numbers from users.
  • Input Method: On mobile devices, type=”tel” can open a numeric keypad with additional symbols such as +, -, and (), making it easier for users to enter phone numbers.
  • Validation: While the tel type does not validate the entered value to confirm that it is a genuine phone number, it can assist enforce the correct format (digits, dashes, and parenthesis).
  • To improve accessibility, name the tel input field (<label>) with a clear purpose. This aids users of screen readers and other assistive technologies in understanding the desired input.
  • Styling: You can use CSS to make the tel input field match the look of your website.

Overall, the tel type is useful for ensuring that users enter phone numbers in a consistent format, especially on mobile devices where specialized input methods might enhance the user experience.

3. Describing the URL Type: In HTML, the <input> element’s url type creates a text input field for URLs (Uniform Resource Locators). Using <input type=”url”> notifies the browser that the intended input is a URL. On some devices, this can result in specialized input techniques, like as keyboards with keys for typical URL characters like., /, and www.

Here’s a brief description of the url type:

  • The url type collects URLs from users.
  • When the URL type is used, some browsers and gadgets may include specialized keyboards or input techniques to let users enter URLs more easily.
  • Validation: The URL type provides simple validation to ensure that the entered value is in the correct URL format. However, it does not verify that the URL exists or is accessible.
  • To improve accessibility, label the URL input field (<label>) clearly to reflect its purpose. This aids users of screen readers and other assistive technologies in understanding the desired input.
  • Styling: You can use CSS to make the url input box match the appearance of your website.

Overall, the url type is useful for ensuring that users enter URLs in a consistent format, and it can improve the input experience on certain devices.

4. Describing the Email Type: In HTML, the <input> element’s email type creates a text input field for email addresses. Using <input type=”email”> indicates that the anticipated input is an email address. This can cause some devices to use specialized input techniques, such as keyboards with keys for typical email characters like @ and ..

Here’s a brief description of the email type:

  • The purpose of the email type is to collect email addresses from users.
  • Input Method: When the email type is used, some browsers and devices may provide specialized keyboards or input techniques to help users enter email addresses faster.
  • Validation: The email type uses basic validation to ensure that the entered value is in the correct email address format. It looks for the @ symbol and a domain name that has at least one. character.
  • To improve accessibility, label the email input field (<label>) with a clear purpose. This aids users of screen readers and other assistive technologies in understanding the desired input.
  • Styling: You can use CSS to make the email input field match the design of your website.

Overall, the email type is useful for ensuring that users enter email addresses in a consistent format, and it can improve the user input experience on specific devices.

5. Describing the Password Type: In HTML, the password type of the <input> element creates a text input field that hides the entered characters. This type is widely used to obtain passwords from users. Using <input type=”password”> hides the user’s inputted characters by showing them as asterisks or dots. Here’s a brief description of the password type:

  • The purpose of the password type is to gather passwords from users.
  • Input Masking: Characters placed into a password input field are usually masked to prevent them from being seen. This is a security mechanism that prevents others from seeing the password.
  • To improve accessibility, clearly label the password entry area (<label>) to identify its purpose. This aids users of screen readers and other assistive technologies in understanding the desired input.
  • Styling: You can use CSS to make the password input box match the appearance of your website.
  • However, because the characters are masked, visual design may have little effect on the appearance of the input area.

Overall, the password type is useful for securely collecting passwords because it hides the characters supplied by the user and prevents others from seeing them.

6. Describing the datetime-local Type: The datetime-local type of the <input> element in HTML creates a date and time input field that allows users to select a date and time without a time zone. When <input type=”datetime-local”> is used, it offers a calendar and time picker interface to select the date and time. Here’s a brief description of the datetime-local type:

  • The purpose of the datetime-local type is to collect date and time values from users without providing a timezone.
  • Input Format: The format of the input value for datetime-local is YYYY-MM-DD.HH:MM, where YYYY is the year, MM is the month, DD is the day, HH is the hour (in 24-hour format), and MM is a minute.
  • To improve accessibility, name the datetime-local input field (<label>) clearly to reflect its purpose. This enables screen readers and other assistive technology to comprehend the desired input.
  • Styling: You can use CSS to customize the datetime-local input field to match your website’s appearance.
  • Compatibility: Most modern browsers support the datetime-local type; however, older browsers and some mobile browsers may not. It is usually a good idea to test the functionality in many browsers to verify compatibility.

Overall, the datetime-local type is beneficial for capturing date and time values from users in a specified format, allowing them to quickly pick both the date and time.

7. Describing the datetime, date, month, week, and time Type: The <input> element in HTML can be combined with several kinds to generate date and time input fields, each with a distinct function. Below is a description of each type:

  • Datetime Type (type=”datetime”): The datetime type creates a text input field where you can enter a date and time, as well as a time zone offset. Example: <input type=”datetime” name=”meeting_datetime”>
  • Date Type (type=”date”): The date type creates a text input field where you can insert a date (without time). Example: <input type=”date” name=”event_date”>
  • Month Type (type=”month”): The month type generates a text input area for entering the month and year. Example: <input type=”month” name=”birth_month”>
  • Week Type (type=”week”): The week type generates a text input area for entering the week and year. Example: <input type=”week” name=”vacation_week”>
  • Time Type (type=”time”): The time type creates a text input field where you can enter a time (without a date). Example: <input type=”time” name=”appointment_time”>

These input types offer a user-friendly way to enter date and time values, with each focused on a unique feature. They can be designed with CSS to match your website’s look and can include properties such as necessary for validation.

8. Describing the number and range Type: To construct number input fields, use the <input> element in HTML with the type property set to number or range. Each field serves a specific purpose. Below is a description of each type:

  • Number Type (type=”number”): The number type generates a text input box for entering a numerical value. It contains controls (such as up and down arrows) for increasing or decreasing the value. Example: <input type=”number” name=”quantity” minimum=”1″ maximum=”10″>
  • Range Type (type=”range”): The range type generates a slider control for picking a numeric value from a given range. It is handy for picking a value from a continuous range (for example, volume level or temperature). Example: <input type=”range” name=”volume” min=”0″ max=”100″>

Both types allow you to provide minimum (min) and maximum (max) values, as well as a step (step) value, which defines the increments or decrements when using the controls. They can be designed with CSS to match your website’s look and can include properties such as necessary for validation.

9. Describing the file Type: A file upload control is created using the HTML <input> element’s file type. Using <input type=”file”> users can pick and upload files from their device to a server. Here is a description of the file type.

  • The file type’s purpose is to upload files from the user’s device to the server. When a user clicks on the file input area, a file dialogue box appears, allowing them to navigate their device’s file system and select one or more files to upload.
  • Accept property: Use the accepted property to define which file formats the input field should accept.
  • Accept=”image/*” is an example of how to only accept images. However, this attribute is merely a hint to the browser and does not impose the restriction. The multiple property enables users to pick numerous files for upload.
  • To improve accessibility, name the file entry field (<label>) with a clear purpose. This aids users of screen readers and other assistive technologies in understanding the desired input.
  • Styling: Due to security concerns, the appearance of the file input field can only be modified in limited ways. You can, however, use CSS and JavaScript to hide the default appearance and build your custom-styled file input.

Overall, the file type allows users to upload files to a website, such as photographs, documents, or media files.

10. Describing the Hidden Type: The hidden type of the <input> element in HTML creates a hidden input field that can still be utilized to convey information to the server when a form is submitted. Here is a description of the hidden type.

  • The purpose of the hidden type is to contain values that should be transmitted to the server when a form is submitted but should not be visible or editable to the user.
  • Usage: Hidden input fields are frequently used to store session tokens, user IDs, and other information that must be provided along with form data.
  • Accessibility: Because hidden input fields are not visible on the web page, they are inaccessible to users and should not be utilized to transmit vital information that users must engage with.
  • Styling: Hidden input fields cannot be styled with CSS because they are not displayed on the website. However, if necessary, you can hide them using CSS.
  • Security Considerations: Hidden input fields should not be utilized to hold sensitive information because the values can be easily accessed by scrutinizing the page source. If you need to keep sensitive data, consider using session variables or other server-side techniques.

Overall, the hidden type is excellent for storing and transferring information that should be included in form submissions but does not need to be visible or modifiable to users.

11. Describing the Checkbox Type: The HTML <input> element’s checkbox type creates a control that allows users to pick one or more alternatives from a list. This is an explanation of the checkbox type.

  • The checkbox type is used to give users a binary choice (checked or unchecked) for each option. Checkboxes are commonly used in forms where users can select various alternatives, such as choosing their favorite colors or agreeing to the terms and conditions.
  • Checkboxes allow for multiple selections, thus users can check several checkboxes to pick more than one option.
  • Accessibility: Each checkbox should have a clear label (<label>) indicating its purpose. This aids users of screen readers and other assistive tools in understanding their selections.
  • Checkboxes can be styled with CSS to match the overall appearance of your website. You can change the appearance of the checkbox and its label.
  • Validation: Unlike other input types, checkboxes do not have a necessary property. If you want to ensure that at least one checkbox is checked, you can utilize JavaScript validation.

Overall, the checkbox type is useful for providing consumers with several alternatives and allowing them to choose one or more.

12. Describing the Radio Type: The radio type of the <input> element in HTML creates a radio button control that allows users to select one option from a set of options. Here’s an explanation of the radio type:

  • The radio type is used to provide users with a list of options from which only one can be picked at a time.
  • Radio buttons are commonly used in forms that require users to select a single option, such as a gender or a payment method.
  • Grouping: Radio buttons are grouped by assigning the same name attribute. This ensures that only one radio button from the group can be selected at a time.
  • Accessibility: Each radio button should have a clear label (<label>) indicating its purpose. This helps users of screen readers and other assistive tools comprehend their options.
  • Styling: Radio buttons can be styled with CSS to match your website’s appearance. You can change the appearance of both the radio button itself and its label.
  • Validation: Unlike other input types, radio buttons do not have a necessary property. If you want to ensure that just one radio button is selected, you can use JavaScript validation.

Overall, the radio type is useful for providing consumers with a list of exclusive possibilities and allowing them to choose one from the list.

13. Describing the Submit Type:

The HTML <input> element’s submit type generates a submit button that sends form data to the server upon click. Here is a description of the submission type:

  • The purpose of the submit type is to construct a button that, when clicked, sends the form data to the server for processing.
  • Submit buttons are often used at the end of a form to allow visitors to enter their information, such as filling out a registration form or submitting a search query. When a submit button is hit, the browser validates the form data (if any is supplied) before sending it to the server specified in the form’s action property.
  • Accessibility: Make sure the submit button has a clear caption indicating its purpose. This makes it easier for screen readers and other assistive technology to understand how the button functions.
  • Submit buttons can be modified with CSS to match the overall appearance of your website. You can change the appearance of the button, including its size, color, and text.
  • Validation: If form validation is required, you can use JavaScript to validate the form input before submission. This can help guarantee that the data is correct and complete before being delivered to the server.

Overall, the submit type is required to allow users to submit form data, initiate the submission process, and enable server-side processing of the input.

14. Describing the reset Type: The <input> element’s reset type creates a button that resets the form’s controls to their initial values when clicked. Here’s an explanation of the reset type:

  • The reset type creates a button that resets all form controls within a <form> element to their initial values.
  • Reset buttons are commonly used alongside submit buttons to allow users to reset the form and start again or undo their modifications.
  • Clicking the reset button resets all form controls within the same <form> element to their initial values. This contains text inputs, checkboxes, radio buttons, and dropdown menus.
  • Accessibility: Make sure the reset button has a clear label indicating its purpose. This makes it easier for screen readers and other assistive technology to understand how the button functions.
  • Reset buttons can be styled with CSS to match your website’s appearance. You can change the appearance of the button, including its size, color, and text.
  • Caution: Using a reset button may result in data loss, particularly if the form contains unsaved user input. Consider implementing confirmation dialogues or other techniques to prevent inadvertent form resets.

Overall, the reset type is useful because it allows users to quickly reset form controls to their original settings, allowing them to undo modifications or start over.

Exploring the Button Element

HTML’s <button> element is a versatile and important element for creating interactive buttons on web pages. It enables developers to design buttons with a variety of functionality, including launching JavaScript activities, submitting forms, going to other pages, and more. Unlike the <input> element with type=”submit” or type=”reset”, the element can contain rich content, such as text, graphics, or even other HTML components, making it very customizable and adaptable.

The <button> element can manage complex interactions and behaviors, making it a valuable tool. For example, you can utilize JavaScript event handlers like on click to run custom scripts when a button is pressed, providing for dynamic and interactive user experiences. This makes it an effective tool for designing interactive forms, modal dialogues, and interactive widgets for websites. Furthermore, the element has other features that improve its functioning and accessibility. For example, the type property can be set to submit to generate a form submit button, reset to produce a form reset button, or button (default) to create a generic button that can be used to do custom actions. The disabled attribute can also be used to deactivate a button and prohibit people from clicking on it.

CSS can be used to style the <button> element to align with a website’s branding and design. CSS properties such as background-color, border, padding, and text-align can be used to customize the appearance of the button, ensuring that it is visually pleasing and compatible with the website’s overall style. In terms of accessibility, buttons should have meaningful text labels to ensure that they are understandable to screen readers and other assistive technology. Furthermore, employing ARIA properties like as aria-label or aria-labeled can help improve button accessibility by adding context or labeling.

The <button> element is a key component for designing interactive and user-friendly web interfaces. Its versatility, functionality, and accessibility make it an indispensable tool for web developers seeking to create interesting and dynamic web experiences.

AttributeDescriptionNew in HTML5
autofocusallows the button control to get the focus as soon as the page loads.yes
disableddisables the button control.no
formrefers to the id of the FORM element.yes
formactionrefers to the value of the action attribute of the current form.yes
formenctypespecifies a value that is used to encode the content while submitting to the server. the possible values are application/x-www-form-urlencoded (default), multipart/form-data, text/plain.yes
formmethodspecifies the method of the HTTP at the time of the submitting the button control. the possible values are get, post, and delete.yes
formnovalidatespecifies that the form is not to validate at the time of submitting the button control.yes
formtargetspecifies the destination, such as a new tab or a new window, to load the browsing context.yes
nameprovides a name to the button control.no
typespecifies the type of the button control. the possible values are submit, reset, and button.no
valueprovides a value to the button control. you can use this attribute if the form attribute is present.no
Table 2 – Attributes of the BUTTON Element.
Exploring the Multiple-Choice Elements

HTML’s multiple-choice elements, including checkboxes (<input type=”checkbox”>), radio buttons (<input type=”radio”>), and choose dropdowns (<select> with <option> elements), enable users to select one or more alternatives from a list. Checkboxes offer several selections, but radio buttons only allow one. Select dropdowns display a list of items in a dropdown menu that may be selected. These elements can be customized using CSS to match your website’s appearance and should be labeled clearly for accessibility. Let’s now discuss these multiple-choice elements in detail starting with the SELECT Element:

1. The SELECT Element: The <select> element in HTML creates a dropdown list of possibilities, allowing users to select one from the list. It is widely used in forms to present users with a list of predefined options for a specific field. The <select> element may include many <option> components, each representing a dropdown list choice. The name attribute gives the name of the select element, which will be used to identify the selected option when the form is submitted. Each <option> element contains a value attribute, which is the value delivered to the server when the form is submitted and the option is selected. The text between the opening and closing <option> tags represents the choice’s visible text in the dropdown list. CSS allows for customization of the <select> element’s look, such as size, font, colors, and borders. You can also use JavaScript to dynamically update the dropdown list’s selections in response to user interactions or other events. The <select> element is an effective tool for designing user-friendly online forms and interfaces.

AttributeDescriptionNew in HTML5
disabledimplies that the drop-down list is disabled.no
namerefers to the name of the drop-down list.no
sizerefers to the number of visible options shown in the drop-down listno
autofocusallows the button control to get the focus as soon as the page loads.yes
formrefers to the ID of the FORM.yes
multiplespecifies that multiple items can be selected from the drop-down list.no
Table 3 – Attributes of the SELECT Element

2. The OPTION Element: To define specific options in a dropdown list, use the <option> element in HTML within a <select>. Each <option> represents a choice the user can select. The <option> element’s value attribute defines the value provided to the server when the form with the <select> element is submitted if the option is selected. The text between the opening and closing tags represents the dropdown list option’s visible text.

AttributeDescription
labelrefers to the heading of the several groups.
disableddisables the option element.
selectedrefers to the option that is to be displayed as default.
valuerefers to the value that is sent to the server.
Table 4 – Attributes of the OPTION Element

3. The OPTGROUP Element: The <optgroup> HTML element organizes similar options in a <select> dropdown list. It improves choice organization and categorization, allowing users to identify and select the required option more easily. The <optgroup> element includes a label attribute that provides the group name displayed in the dropdown list. Each <optgroup> can include one or more <option> elements, each indicating a unique choice inside the group.

AttributeDescription
labelrefers to the heading of the several groups in the cascading menu.
disableddisables the OPTGROUP element.
Table 5 – Attributes of the optgroup Element
Exploring the TEXTAREA and LABEL Elements

The textarea> element in HTML creates a multi-line text input field that allows users to submit lengthier text values, such as comments and messages. Unlike the single-line <input type=”text”> element, <textarea> can span multiple lines, making it useful for capturing longer amounts of text. The <textarea> element is commonly used within <form> elements to capture user input, which is then submitted to a server.

Here’s an example of a <textarea> element:

This example uses the <label> element to label the <textaera>, making it more accessible. To associate the label with the input field, the <label>’s or attribute must match the id attribute of the <textarea>. The <textarea>element’s rows and cols attributes provide its initial size in terms of rows and columns.

The <label> element in HTML defines a label for a <input>, <textarea>, <select>, and <button> element. It improves accessibility by linking the label text with the form controls. When a user clicks on an element connected with an input field, the browser focuses on the input field, facilitating form interaction. Here’s an example of an element associated with an <input> element:

In this example, pressing the “Username:” label will concentrate the matching element, allowing the user to start typing without explicitly clicking on the input field. This behavior enhances usability, particularly for users who have trouble clicking on small targets.

AttributeDescriptionNew in HTML5
colsrefers to the visible width of the textarea control.no
rowsrefers to the permitted number of rows in the textarea control.no
disableddisables the textarea.no
namerefers to the name of the textarea.no
readonlyspecifies that the textarea is read-only and you cannot write in it.no
accesskeyrefers to the shortcut key on the keyboard.no
autofocusallows the control to get the focus as soon as the page loads.yes
dirnamespecifies the name of the input control that indicates the text direction of the textarea.yes
maxlengthspecifies the maximum number of characters that can be entered in the textarea.yes
placeholderhelps the user to fill the respective textarea by providing a hint for the input.yes
requiredspecifies that the value of the input field is required to submit the form.yes
wrapallows the textarea to wrap the text.yes
Table 6 – Attribute of the TEXTAREA Element

Some controls, such as button controls, do not require a description since they already have labels; however, others, such as text boxes, check boxes, and radio buttons, must. You can explain the control by adding a label to the form with the LABEL element. Each LABEL element is connected with only one control.

AttributeDescription
forassociates the label with a specific control. the value of this attribute must match the id attribute of its associated control.
formrefers to the id of a form.
Table 7 – Attribute of the LABEL Element
Exploring the FIELDSET and LEGEND Elements

The <fieldset> element in HTML organizes related form components visually and semantically. It is frequently used to group radio buttons, checkboxes, and text inputs. The <legend> element adds a caption or title to the <fieldset> element, defining the purpose or context of the grouped form elements. Here’s an example of how <fieldset> and <legend> can be used together:

In this example, the <fieldset> element groups the name and email input fields together under the caption “Contact Information” provided by the element. This grouping helps users comprehend the relationship between the form fields while also improving the form’s accessibility and usefulness.

The <fieldset> element can be combined with the <legend> element to create collapsible sections or fieldsets within a form. This allows users to conceal or show relevant form elements based on their interactions or preferences.

AttributeDescriptionNew in HTML5
disableddisables the textarea.yes
namerefers to the name of the textarea.yes
formrefers to the id of a form.yes
Table 8 – Attribute of the FIELDSET Element

Describing the DATALIST Element

The <datalist> element in HTML provides a list of predefined alternatives for a <input> element with the list property. It lets users select a value from a list of options while also allowing them to submit their value if necessary. The <datalist> element is commonly used with text input fields to enable autocomplete capabilities. Here’s an example of how <datalist> can be used:

In this example, the <input> element contains a listed property that points to the <datalist> element with the id “browsers”. Associating the <datalist> with the input field allows the browser to deliver autocomplete suggestions based on the <datalist>’s defined options.

The <datalist> element can include one or more elements, each representing a possible value for the input field. When a user puts into the input box, the browser shows a dropdown list of options depending on the <datalist> content, allowing them to select one of the predefined alternatives or enter their value.

The <datalist> element enhances the user experience of text input fields by offering autocomplete suggestions based on predefined alternatives.

Describing the KEYGEN Element

HTML’s <keygen> element generates a public and private key pair for forms. It was primarily used to generate public key infrastructure (PKI) keys, such as SSL/TLS certificates, for web-based communication security. The <keygen> element was deprecated and removed from the HTML standard for security and usability reasons.

To generate keys and manage encryption, consider utilizing the Web Crypto API in JavaScript rather than <keygen>. This API offers a more secure and adaptable approach to cryptographic operations in web applications.

AttributeDescriptionNew in HTML5
autofocusallows the control to get the focus as soon as the page loads.yes
challengespecifies a string that is used for the verification at the time of submission of the form.yes
disableddisables the input control.yes
formrefers to the id of the FORM Element.yes
keytypespecifies the type of the key to generate.yes
nameprovides a name to the input control.yes
Table 9 – Attributes of the KEYGEN Element

Describing the OUTPUT Element

In HTML, the <output> element represents the outcome of a calculation or user action. It is generally used within a <form> element to display the outcome of a calculation performed based on the user input. The <output> element can be used to show the output of a script or dynamic content. CSS allows for customization of the <output> element’s appearance, including font size, color, and alignment. It is an effective element for providing feedback to users in interactive forms or showing dynamic content on a web page.

AttributeDescriptionNew in HTML5
forassociates the output with a specific control. the value of this attribute must match the id attribute of its associated control.yes
formrefers to the id of a form.yes
namespecifies the name of the OUTPUT element.yes
Table 10 – Attributes of the OUTPUT Element

Describing the PROGRESS Element

In HTML, the <progress> element represents a task’s progress or completion state. It is generally used to graphically track the progress of file uploads, form submissions, and other time-consuming tasks. The <progress> element has two attributes: value (current value) and max (maximum value). CSS allows for customization of the <progress> element’s look, including color, width, and height. JavaScript may dynamically change the value attribute of the <progress> element to show task progress in real-time. The <progress> element provides visual feedback to users on the status of a task or operation on a web page.

AttributeDescriptionNew in HTML5
valuespecifies the value of the PROGRESS element.yes
maxspecifies the maximum value of the PROGRESS element.yes
Table 11 – Attributes of the PROGRESS Element

Describing the METER Element

The <meter> HTML element represents a scalar measurement within a known range. It is commonly used to display metrics like disc usage, completion percentages, ratings, or any other statistic that can be measured on a continuum. The <meter> element includes characteristics like value, min, max, low, high, and optimal to indicate the current value, range minimum and maximum values, and thresholds for distinct states (low, high, and optimum). CSS can be used to customize the appearance of the <meter> element, including the meter bar, fill color, and text content. JavaScript can dynamically change the <meter> element’s value attribute to reflect changing numbers or provide interactivity.

The <meter> element is effective for visually portraying measurements and values within a specific range on a web page.

AttributeDesriptionNew in HTML5
valuespecifies the value of the METER element.yes
minspecifies the minimum value of the METER element.yes
maxspecifies the maximum value of the METER element.yes
lowspecifies a range that is considered as the low value.yes
highspecifies a range that is considered a high value.yes
optimumspecifies a range that is considered the optimum or the best value.yes
formrefers to the id of the FORM element.yes
Table 12 – Attributes of the METER Element

Submitting a Form

Submitting an HTML form entails a set of processes that allow users to enter information and transmit it to a server for processing. The process begins with constructing a form using the <form> element and specifying characteristics such as action (the URL to which the data will be submitted) and method (usually GET or POST). To capture user data, the form uses a variety of input elements such as text fields, checkboxes, radio buttons, and buttons. When the user fills out the form and clicks the submit button, the browser saves all of the form data and sends it to the server. If the method is GET, the form data is added to the URL as query parameters. If the method is set to POST, the form data is transmitted as the HTTP request’s body.

The server receives and processes form data. This may include validating the data, saving it to a database, sending an email, or taking any other action based on the form input. The server then returns a response to the client, which may include a success message, an error message, or a referral to another page. In addition to server-side processing, JavaScript can be used to validate form data before submission. This can assist improve the user experience by giving the user fast feedback if the form contains any problems.

Overall, submitting a form is an essential component of web development, which entails generating a form, gathering user input, sending the data to a server, processing it, and handling the response.

1. The enctype Attribute: The enctype element in HTML forms specifies how form data should be encoded before being delivered to the server. It is commonly used when the form contains file uploads (<input type=”file”>), as standard form submissions (enctype=”application/x-www-form-urlencoded”) cannot handle them.

The enctype attribute has three possible values:

  • The default encoding type for forms is application/x-www-form-urlencoded. Data is encoded as key-value pairs separated by ‘&’ characters, with spaces represented as ‘+’ characters. This works for most form submissions that do not include file uploads.
  • multipart/form-data: This encoding is used when a form includes file uploads. The form data is separated into several sections, each comprising a portion of the form data. This is more sophisticated than application/x-www-form-urlencoded, but it’s required for file uploads.
  • text/plain: This encoding type is rarely used and just sends the form data as plain text, with spaces encoded as ‘%20’. It is not widely used since it provides no advantage over application/x-www-form-urlencoded.

When creating a form with file uploads, you should set the enctype property to multipart/form-data to guarantee that the file data is properly encoded and sent to the server. For standard form submissions without file uploads, the enctype property is usually unnecessary because the default application/x-www-form-urlencoded encoding is adequate.

2. The action Attribute: The action property in HTML forms defines the URL to which the form data should be sent when the form is submitted. It controls where the browser delivers form data for processing. The action attribute’s value can be either a relative URL (to the current page) or an absolute URL (fully qualified URL). When a user submits a form, the browser requests the URL provided by the action attribute. The server then examines the form data and delivers a response, which may be a new web page, a redirect to another page, or some other action triggered by the form submission.

If no action attribute is supplied, the form data is returned to the form’s original page. This is useful for forms that must be processed and shown on the same page without navigating away. It’s worth noting that the action attribute should always be used in conjunction with the method attribute, which defines the HTTP method to be used when submitting the form (for example, GET or POST). If no method attribute is supplied, GET is used by default.

3. The method Attribute: The method attribute in HTML forms provides the HTTP method to utilize when submitting the form. It specifies how the form data will be transmitted to the server. The two most prevalent methods are GET and POST, however, there are also several less common ones.

  • GET method:
    • If the method property is not given, this method is used by default.
    • Data is added to the URL as query parameters.
    • A limited quantity of data can be transmitted (URL length restrictions).
    • Data is accessible in the URL, making it unsuitable for sensitive data.
    • Used to retrieve data from the server.
  • POST method:
    • The data is supplied in the request body rather than attached to the URL.
    • Can deliver a bigger quantity of data than GET.
    • Suitable for sensitive data because the data is not accessible in the URL.
    • Used to send data to the server, such as form submissions.

Other less-used methods include PUT (to update an existing resource), DELETE (to delete a resource), HEAD (to retrieve header information), settings (to retrieve server settings), and TRACE (to run a message loop-back test). These approaches are utilized in special situations and are less frequently used in form submissions. For more examples visit – immediate-solutions-for-working-with-forms

Scroll to Top