Simple Methods for Creating Forms in HTML

Here are some Simple Methods for Creating Forms in HTML: To begin, first refer to – working-with-forms

Creating a Form:

HTML forms use the <form> element and numerous input components to collect user data. Here’s an elementary example of how to make a simple form:

Figure 1 – Displaying the Output of a Form on a Web Page

This example shows a simple form with three input fields: name, email, and message. The action parameter defines the URL where the form data should be submitted, while the method attribute specifies the HTTP method (post in this case). When the user completes the form and hits the “Submit” button, the form data is transmitted to the server specified in the action property using the POST method. The server can then process the form information and respond appropriately.

This is a simple example, but forms can be far more complicated, with several input types, validation, and styling.

Working with the INPUT Element:

HTML’s <input> element creates interactive controls for web forms. It allows you to construct a variety of input fields, including text fields, checkboxes, radio buttons, buttons, and more. The element’s type attribute determines the sort of control created. Here’s an example of using the <input> element to build a simple form with several types of input fields:

Figure 2 – Displaying the Output of the INPUT Element

In this example, we have a form with the following types of input fields:

  • Enter your name and email address as text.
  • Password input.
  • Enter a date for the birthdate.
  • Use the color input to select a color.
  • Number input for choosing a quantity from a range.
  • File input for uploading a file.
  • Checkbox for agreeing to the terms.
  • Click the Submit button to submit the form.

Each input field is labeled for accessibility, and certain input fields include additional properties like min, and max, that are needed to establish limitations and requirements for user input.

Working with a Text Field

To work with a text field in HTML, use the <input> element with the type=”text” attribute. information fields are typically used for one-line input, such as providing a name, email address, or other brief information. This is a detailed example of how to construct and use a text field in an HTML form:

Figure 3 – Displaying the Output of the Text Field

In this example:

  • We have two text areas for inputting your name and email address.
  • The <input> element’s type=”text” attribute provides a single-line text input field.
  • The id attribute associates the element with the element for accessibility.
  • The name attribute identifies the input field when the form is submitted.
  • We’ve introduced the placeholder attribute to provide the user with a tip about what to type into the form.
  • The necessary property is used to make the fields mandatory.

When the form is submitted, the information entered in the text fields is transmitted to the server for processing. The server-side code can then access the data by calling the names supplied in the input fields’ name attributes.

Working with a Password Field

To work with a password field in HTML, use the <input> element with type=”password” attribute. Password fields are used to insert passwords or other sensitive information that should be obscured. This is a detailed example of how to construct and use a password field in an HTML form:

Figure 4 – Displaying the Output of the Password Field

In this example:

  • We have a password field where users can enter their password.
  • The type=”password” attribute on the <input> element provides a password input field that masks the typed content.
  • The id attribute associates the element with the <input> element for accessibility.
  • The name attribute identifies the input field when the form is submitted.
  • We’ve introduced the placeholder attribute to provide the user with a tip about what to type into the form.
  • The necessary property is used to make the field mandatory.

When the form is submitted, the information supplied in the password box is transmitted to the server for processing. It is vital to note that the password data is delivered in plain text and should be encrypted on the server for security reasons.

Creating a Hidden Field

To create a hidden field in HTML, use the <input> element with the type=”hidden” attribute. Hidden fields are used to hold data that the user does not see but is sent to the server when the form is submitted. This is handy for transmitting information such as user IDs, session tokens, and other data that the user should not be allowed to see or alter. Here’s a detailed example of how to create and use a hidden field in an HTML form:

Figure 5 – Displaying the output of the hidden field

In this example, we’ve defined two fields: user name and country. We modified the nation field’s type to hidden and set the default value to India. Figure 5 shows the output of constructing a hidden field.html file. As you can see, the web page just displays one field with a button control. Now, enter your name into the user name form and click the submit button. In this situation, we entered XYZ. As seen in Figure 5, this shows an alert box with the user name and the country name. The alert box retrieves the user’s name from the user name field and the country name from the hidden field.

Creating a Checkbox Field

To create a checkbox field in HTML, use the <input> element with the type=”checkbox” attribute. Checkboxes enable users to select one or more items from a list of alternatives. Here’s an example of how to add a checkbox field to an HTML form:

Figure 6 – Displaying the Output of the Checkbox Field

In this example:

  • We have three checkbox fields, each with a unique ID, name, and value.
  • The id attribute links the element with the checkbox for accessibility.
  • The name property identifies the checkbox field when the form is submitted.
  • The value attribute defines what value will be transmitted to the server if the checkbox is selected.

When you submit the form, the server will receive the values of the checked checkboxes. If a checkbox is unchecked, the value is not included in the form data transmitted to the server.

Creating a Radio Button Field

To create a radio button field in HTML, use the <input> element with the type=”radio” attribute. Radio buttons allow users to choose one of several mutually exclusive options. Here’s an example of how to add a radio button field in an HTML form:

Figure 7 – Displaying the Output of the Radio Button Field

In this example:

  • We have three radio button fields, each with a unique ID, name, and value.
  • The id attribute links the <label> element to the radio button for accessibility.
  • The name attribute is used to group radio buttons together. Only one radio button from a group can be chosen at a time.
  • The value attribute specifies the value that will be delivered to the server when the radio button is selected.

When the form is submitted, the server receives the value of the selected radio button in the gender field. If no radio button is selected, no value will be provided to the gender column.

Creating a Submit Button Field

To create a submit button in HTML, use the <input> element with the type=”submit” property. Submit buttons are used to send forms to the server for processing. Here’s an example of how to add a submit button to an HTML form:

Figure 8 – Displaying the Output of the Submit Button Field

In this example:

  • We generated a submit button using the <input> element with type=”submit”.
  • The value attribute of the submit button determines what text will be displayed on the button.
  • When you click the submit button, the form data (in this case, the values of the name and email fields) is transferred to the server specified in the form’s action property for processing.

Submitting the form usually results in a page reload or a redirect to another page, depending on the server’s response.

Creating a Reset Button Field

To create a reset button in HTML, use the <input> element with the type=”reset” property. Reset buttons are used to reset form fields to their original values. Here’s an example of creating a reset button in an HTML form:

Figure 9 – Displaying the Output of the Reset Button Field

In this example:

  • We generated a reset button using the element with type=”reset”.
  • The value attribute of the reset button specifies what text will be displayed on the button.
  • When you click the reset button, all of the form fields (in this case, the name and email fields) are reset to their default settings, as if the form were just loaded.

Reset buttons are important for allowing users to clear the form and start over without having to manually clear each field.

Using the SELECT and OPTION Elements

The <select> element in HTML creates a drop-down list of options for the user to select from. Each element can have one or more <option> elements that specify the dropdown list’s options. Here’s an example of using the <select> and <option> elements to build a dropdown list:

Figure 10 – Displaying the Output of the SELECT and OPTION Elements

In this example:

The <select> element with the id cars and the name vehicles creates a drop-down list for picking an automobile. The <select> element has four elements, each with a value property that defines the value to be transmitted to the server if the option is picked, as well as the text presented to the user.

When you submit the form, the selected value from the drop-down list (Volvo, Saab, Fiat, or Audi) is transmitted to the server in the vehicles field.

Using the OPTGROUP Element

The <optgroup> element in HTML groups similar <option> components within a <select> element. It enables you to establish a hierarchical structure in a drop-down list, making it easier for users to discover and select the appropriate option. Here’s an example of using the <optgroup> element to group options in a dropdown list:

Figure 11 – Displaying the Output of the optgroup Element

In this example:

The <select> element with the id cars and the name vehicles creates a drop-down list for picking an automobile. The <select> element has two <optgroup> elements, each with a label property to specify the group’s label.

  • Each <optgroup> element contains <option> elements, which define the group’s options.
  • When you submit the form, the selected value from the drop-down list (Volvo, Saab, Mercedes, or Audi) is transmitted to the server in the vehicles field.

Working with the TEXTAREA Element

The <textarea> HTML element creates a multi-line text input field. It’s handy for allowing users to enter lengthy content, such as comments or messages. Here’s an example of using the <textarea> element:

Figure 12 – Displaying the Output of the TEXTAREA Element

In this example:

  • We have a <textarea> element with the id and name set to message. The <textarea> element’s rows=”4″ and cols=”50″ parameters determine its initial size. Users can add multiple lines of text in the text area, and the text will be sent to the server as the message field’s value when the form is submitted.
  • You can customize the <textarea> element by changing its initial value, deactivating it, or inserting placeholder text, among other choices.

Using the FIELDSET and LEGEND Elements

In HTML, the <fieldset> element groups similar components within a form and adds a border to visually differentiate them from other elements. The <legend> element specifies a caption or title for the <fieldset> element:

Here’s an example of how to use the <fieldset> and <legend> elements:

Figure 13 – Displaying the output of the FIELDSET and LEGEND Elements

  • In this illustration, there are two <fieldset> elements, each of which contains form components about a distinct area of the form.
  • A <legend> element is present in every <fieldset> element, serving as a caption or title for the collection of form elements.
  • By assembling related items into groups and defining a visual border around them, the <fieldset> elements aid in the organization of the form and enhance its design.

It is very helpful to use the <fieldset> and <legend> components if your form has several sections or groups of related fields.

Using the BUTTON Element

A clickable button in HTML is made with the <button> element. It can be used to submit forms, start JavaScript processes, or carry out any other JavaScript-capable operation. The following demonstrates the usage of the <button> element:

Figure 14 – Displaying the output of the BUTTON Element

  • In this example, a <button> element with the word “Click Me” as its content has an onclick property that, when the button is clicked, calls the JavaScript method handleClick(). An alert with the message “Button clicked!” is displayed by this function.
  • Moreover, a <form> element has two text input fields for the user’s name and email address. Because it has the type=”submit” attribute, the <button> element inside the form is a submit button. The form is sent to the server listed in the action attribute of the form when this button is pressed.

Two typical uses for the element are shown in this example: submitting a form and starting a JavaScript function when clicked.

Figure 15 – Showing the Image on the Button Control

  • An <img> element and a <button> element are used in this example.
  • The <img> element contains an alt attribute that offers alternate text for the picture and a src attribute that indicates the actual image or URL to display.
  • The value of the src attribute can be changed to point to the real URL of the picture you wish to see on the button.

Using the DATALIST Element

When an <input> element has the list property, the <datalist> element in HTML is used to supply a predetermined list of alternatives. Users can choose a value from a list of recommended possibilities, and if necessary, they can still enter their value. The following demonstrates the usage of the <datalist> element:

Figure 16 – Displaying the output of the Datalist Element

  • In this instance, the list attribute of the <input> element is set to “browsers.” This links the <datalist> element and the input field.
  • Each of the <option> components in the list of <datalist> elements represents a proposed choice for the input field. The id of the list of elements is “browsers.” Based on the choices in the <datalist>, a drop-down list of recommended options will appear when the user interacts with the input field. The user has the choice to enter their value or choose one of the options.

When it comes to giving text input fields autocomplete functionality—which allows users to rapidly choose from a list of commonly used values—the <datalist> element is especially helpful

Using the OUTPUT Element

The HTML <output> element is used to display the outcome of a computation or user input. To show dynamic material based on user input or other interactions, it is usually used in conjunction with JavaScript. The following demonstrates the usage of the <output> element:

Figure 17 – Displaying the output of the OUTPUT Element

Figure 18 – Showing the output after Adding the Two Numbers

In this example, the <button> element triggers the JavaScript function calculate() when clicked, and there are two <input> elements for entering numbers. The <output> element’s content is set to the result’s id by the calculate() function, which also obtains the values entered in the <input> elements, adds up the numbers, and displays the result.

The total of the numbers entered in the input fields and the “Calculate Sum” button click will be shown in the <output> element.

Using the PROGRESS Element

When a task, like the completion of a download or the submission of a form, is in progress, it is represented using the <progress> element in HTML. To dynamically update the progress value, it is usually used in conjunction with JavaScript.
The following demonstrates the usage of the <progress> element:

Figure 19 – Displaying the output of the PROGRESS Element.

Figure 20 – Showing that the File has been uploaded

In this instance: to choose a file to upload, use the <input> element of type “file.” With the id “progress,” starting value “0” and maximum value “100,” we have an <progress> element. The file upload’s progress is shown by this element. To imitate the file upload procedure, we have a <button> element that, when clicked, launches the JavaScript method uploadFile(). Based on the file size, the uploadFile() function determines how many chunks to upload and modifies the progress value accordingly. The upload progress is updated every second until it is finished.

Please be aware that this sample is just meant to serve as an example; no file is uploaded to a server. You would need to use server-side programming to manage the file upload in a real application.

Using the METER Element

A scalar measurement within a defined range is represented by the <meter> element in HTML. Values like disc usage, completion percentage, and other quantitative data are frequently visualized using it. The following demonstrates the usage of the <meter> element:

Figure 21 – Displaying the Output of the METER Element

In this case, selecting the “Update” button triggers the JavaScript function updateMeter(), which modifies the <meter> element with id “disc” following a random value between 0 and 100.

Using the Enctype, Action, and Method Attributes

HTML forms use the enctype, action, and method elements to specify how form data is sent to the server.
The encoding type that was used to send the form data to the server is indicated by the enctype attribute. When there are file uploads on the form, it is necessary. Text/plain, multipart/form-data, and application/x-www-form-urlencoded are the most often occurring values. The URL of the server-side script or endpoint that will handle form submission is specified by the action property. The URL may be absolute or relative. The HTTP method used to submit the form data is specified by the method property. Though PUT and DELETE are other viable options, GET and POST are the most often utilized techniques. This form demonstrates how to use these attributes:

Figure 22 – Displaying the output of using the Enctype, Action, and Method Attributes

Now showing the Code for the action.html File:

Figure 23 – Displaying the output of the action.html file

In this case: As indicated in the above graphic, you must develop an HTML page called action.html to be routed to the following form submission. The POST method will be used to submit the form to the server’s /submit_form endpoint. The form data has a file upload input (<input type=”file”>), hence it will be encoded as multipart/form-data. In addition to a file upload area, the form has text input fields for name and email. The data will be passed to the server-side script or endpoint mentioned in the action property for processing after the form is submitted.

Scroll to Top