HTML HTML Tutorial HTML Forms HTML Graphics HTML Media HTML APIs HTML Tags



HTML Tag: form

The HTML tag form is used to create an interactive form on a web page. Forms are used to collect data from users, such as their name, email address, and other information. The form tag is one of the most important tags in HTML, as it allows developers to create dynamic and interactive web pages.

Brief Explanation of HTML Tag: form

The form tag is used to create a form on a web page. The form can contain various input fields, such as text boxes, radio buttons, checkboxes, and drop-down lists. When a user submits the form, the data is sent to a server for processing. The server can then use the data to perform various actions, such as sending an email or updating a database.

The form tag has several attributes that can be used to customize the form. The most common attributes are action, method, and enctype. The action attribute specifies the URL of the script that will process the form data. The method attribute specifies the HTTP method that will be used to submit the form data. The enctype attribute specifies the encoding type that will be used to send the form data.

Code Examples in Per Tags

Text Input Field

The following code creates a text input field:

    <form>
        <label for="name">Name:</label>
        <input type="text" id="name" name="name">
    </form>

The label tag is used to provide a label for the input field. The for attribute of the label tag should match the id attribute of the input field. The type attribute of the input tag should be set to "text" to create a text input field.

Radio Buttons

The following code creates a group of radio buttons:

    <form>
        <label>Gender:</label>
        <input type="radio" id="male" name="gender" value="male">
        <label for="male">Male</label>
        <input type="radio" id="female" name="gender" value="female">
        <label for="female">Female</label>
    </form>

The name attribute of the input tag should be the same for all radio buttons in the group. The value attribute of each radio button should be unique. The label tag is used to provide a label for each radio button. The for attribute of the label tag should match the id attribute of the corresponding radio button.

Checkboxes

The following code creates a group of checkboxes:

    <form>
        <label>Interests:</label>
        <input type="checkbox" id="reading" name="interests" value="reading">
        <label for="reading">Reading</label>
        <input type="checkbox" id="music" name="interests" value="music">
        <label for="music">Music</label>
        <input type="checkbox" id="sports" name="interests" value="sports">
        <label for="sports">Sports</label>
    </form>

The name attribute of the input tag should be the same for all checkboxes in the group. The value attribute of each checkbox should be unique. The label tag is used to provide a label for each checkbox. The for attribute of the label tag should match the id attribute of the corresponding checkbox.

Drop-down List

The following code creates a drop-down list:

    <form>
        <label for="country">Country:</label>
        <select id="country" name="country">
            <option value="usa">USA</option>
            <option value="canada">Canada</option>
            <option value="uk">UK</option>
        </select>
    </form>

The select tag is used to create a drop-down list. The name attribute of the select tag is used to identify the form field. Each option tag represents an item in the list. The value attribute of each option tag should be unique.

References

Activity