Forms in HTML documents
HTML forms are one of the most fundamental components of the web. They allow users to submit data to a server, which can then be processed and used to perform various actions, such as registering for an account, making a purchase, or searching for information.
An HTML form is created using the <form> element, which can have various attributes that determine its behavior and appearance. The most common attributes include:
- action: This attribute specifies the URL of the server-side script that will handle the form data when it is submitted.
- method: This attribute specifies the HTTP method that will be used to submit the form data, which is usually either GET or POST.
- name: This attribute specifies the name of the form, which can be used to reference it in scripts or stylesheets.
- enctype: This attribute specifies the encoding type of the form data that will be submitted to the server, which is typically either application/x-www-form-urlencoded or multipart/form-data.
Inside the <form> element, various input elements can be added to collect data from the user, such as text inputs, radio buttons, checkboxes, select menus, and file uploads. Each input element can have its own attributes that determine its behavior and appearance, such as:
- type: This attribute specifies the type of input element, which can be text, password, radio, checkbox, select, file, and many others.
- name: This attribute specifies the name of the input element, which is used to identify it when the form is submitted to the server.
- value: This attribute specifies the default value of the input element, which can be pre-filled by the server or the user.
- placeholder: This attribute specifies a hint or sample value to display in the input element before the user enters data.
- required: This attribute specifies whether the input element is required, which means that the user must enter data before the form can be submitted.
- readonly: This attribute specifies whether the input element is read-only, which means that the user can view but not edit the data.
When the user submits the form by clicking a submit button or pressing the Enter key, the form data is sent to the server using the specified HTTP method and encoding type. The server-side script can then process the data and perform various actions, such as storing it in a database, sending an email, or generating a response HTML page.
Different types of form controls in HTML
HTML provides a variety of form controls that allow users to input or select data in different ways. Following are some of the most commonly used form controls:
- Text Input: The <input> element with type="text" creates a simple text input field that allows users to enter text.
- Password Input: The <input> element with type="password" creates a text input field that masks the input with asterisks or dots to hide the entered text.
- Radio Button: The <input> element with type="radio" creates a set of radio buttons, of which only one can be selected at a time.
- Checkbox: The <input> element with type="checkbox" creates a checkbox that can be selected or deselected by the user.
- Select Menu: The <select> element creates a dropdown menu that allows users to select one option from a list of options.
- Textarea: The <textarea> element creates a large text input field that allows users to enter multiple lines of text.
- File Upload: The <input> element with type="file" creates a file upload control that allows users to select and upload a file from their local device.
- Hidden Input: The <input> element with type="hidden" creates an invisible input field that can be used to store data that is not displayed to the user.
- Button: The <button> element creates a clickable button that can be used to trigger a function or event on the web page.
- Range Input: The <input> element with type="range" creates a slider control that allows users to select a value within a range.
- Date/Time Input: The <input> element with type="date" creates a control that allows users to select a date, while type="time" creates a control that allows users to select a time.
- Color Input: The <input> element with type="color" creates a control that allows users to select a color.
Above showing controls are just a few of the most common form controls available in HTML. By combining these form controls with various HTML and CSS attributes, developers can create complex and customized forms that meet the needs of their users.
How to create an HTML form?
Creating an HTML form involves several steps, which are described below:
Create a form element
The first step is to create a <form> element in your HTML code, which will contain the form controls and specify how the data will be sent to the server. The most important attributes of the <form> element are action and method. The action attribute specifies the URL of the script that will process the form data on the server, while the method attribute specifies the HTTP method that will be used to send the data to the server, which is usually either GET or POST.
Add form controls
Inside the <form> element, you can add various form controls such as text input fields, radio buttons, checkboxes, select menus, and buttons using appropriate HTML elements like <imput>, <select> and <textarea>. Each form control should have a unique name attribute which will be used to identify the control on the server-side script.
Add form validation
You can add validation to the form controls using HTML5 validation attributes like required and pattern, or by adding custom JavaScript validation code to the form.
Submit the form
When the user clicks the submit button, the form data is sent to the server using the specified method and action. You can add a submit button using the <button> element or the <input> element with type="submit".
These are the basic steps to create an HTML webpage using Form tag.
Add form controls to HTML form
To add form controls to your HTML form, you can use a variety of input elements. Each input element has its own set of attributes that you can use to customize its behavior and appearance. Here are some common input elements and how to use them:
Text input:
This element allows the user to enter text into a form field. You can use the type attribute with a value of "text" to create a text input element.
This code creates a label for the input field with an id attribute of "name" and a name attribute of "name". The id attribute allows you to associate the label with the input field, and the name attribute specifies the name of the form field that will be sent to the server when the form is submitted.
Checkbox
This element allows the user to select one or more options from a list. You can use the type attribute with a value of "checkbox" to create a checkbox input element.
This code creates a label for the checkbox input with an id attribute of "subscribe", a name attribute of "subscribe", and a value attribute of "yes". When the checkbox is checked, the value "yes" will be sent to the server along with the name "subscribe".
Radio button
This element allows the user to select one option from a list. You can use the type attribute with a value of "radio" to create a radio button input element. For example:
This code creates two radio button inputs with different id, name, and value attributes. The name attribute should be the same for both radio buttons, so that only one option can be selected at a time. When the user selects one of the radio buttons, the corresponding value attribute will be sent to the server along with the name "gender".
Select menu
This element allows the user to select one option from a drop-down list. You can use the <select> element to create a select menu, and the <option> element to create each option in the list.
This code creates a label for the select menu with an id attribute of "city", a name attribute of "city", and three options with different value attributes. When the user selects one of the options, the corresponding value attribute will be sent to the server along with the name "city".
What is form validation and how is it done in HTML?
Form validation is the process of ensuring that the data entered into a form by a user is accurate and meets certain criteria before it is submitted to the server. In other words, it helps to prevent users from submitting incomplete or incorrect data to the server.
In HTML, form validation can be done using the built-in form validation attributes and JavaScript. Following are some examples:
Required attribute
You can use the required attribute to make a form field required.
This code creates a text input field with the required attribute. This means that the user must enter a value into this field before they can submit the form.
Pattern attribute
You can use the pattern attribute to specify a regular expression that the user's input must match.
This code creates a text input field for the user to enter their phone number, and uses the pattern attribute to specify that the phone number must be in the format "XXX-XXX-XXXX", where each X represents a digit.
JavaScript validation
You can use JavaScript to perform more complex validation on a form.
This code uses the onsubmit event to call a JavaScript function called validateForm(). This function gets the value of the email input field, and uses a regular expression to check if it is a valid email address. If it is not valid, an alert message is displayed, and the function returns false, which prevents the form from being submitted.
Above showing are just a few examples of how form validation can be done in HTML using the built-in attributes and JavaScript. It's important to remember that form validation should be done both on the client side (using JavaScript) and on the server side, to ensure that the data entered by the user is accurate and secure.
Specify where the form data should be sent
To specify where the form data should be sent, you need to use the action attribute in the <form> element. The action attribute specifies the URL of the server-side script that will process the form data. When the user submits the form, the browser will send the form data to the URL specified in the action attribute using the HTTP method specified in the method attribute.
There are two ways to specify the URL in the action attribute:
Relative URL
You can use a relative URL in the action attribute to specify the path to the server-side script relative to the current page. For example, if the server-side script is located in the same directory as the current page, you can specify the URL as follows:
If the server-side script is located in a different directory, you can specify the path relative to the current page as follows:
Absolute URL:
You can use an absolute URL in the action attribute to specify the full URL of the server-side script, including the protocol, domain, and path. For example:
When the user submits the form, the browser will send the form data to the URL specified in the action attribute. The server-side script can then process the form data and send a response back to the browser, which can be displayed on the current page or on a new page, depending on how you handle the response in the script.
Conclusion:
HTML forms are an essential tool for creating interactive web applications that can collect data from users and perform various actions on the server side. By using the various input elements and form attributes available in HTML, developers can create rich and engaging web forms that meet the needs of their users.