Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Understanding Form Elements (<form>, <input>, <textarea>, <select>)


HTML forms are essential for collecting user input. A form in HTML can include multiple elements such as <input>, <textarea>, and <select> to gather different types of data. This article explains the purpose and use of these form elements.

The <form> Element

The <form> element is the container for various input elements that make up a form. It has attributes like action and method:

Example of a Simple Form:

    <form action="submit_form.php" method="post">
        <input type="text" name="username" placeholder="Enter your username">
        <button type="submit">Submit</button>
    </form>
        

Here, the form sends the username data to submit_form.php using the POST method when the submit button is clicked.

The <input> Element

The <input> element is used to create various input fields in a form. It has a type attribute that specifies the kind of input field, such as "text," "password," "email," or "submit."

Examples of Different <input> Types:

    <form>
        <label>Username:</label>
        <input type="text" name="username"> <br>
        
        <label>Password:</label>
        <input type="password" name="password"> <br>
        
        <label>Email:</label>
        <input type="email" name="email"> <br>
        
        <input type="submit" value="Register">
    </form>
        

This form includes a text field for the username, a password field, an email field, and a submit button to send the form data.

The <textarea> Element

The <textarea> element is used to create a multi-line text input field, which is useful for entering longer pieces of text, such as comments or descriptions. It does not use a type attribute.

Example of a <textarea>:

    <form>
        <label>Comments:</label> <br>
        <textarea name="comments" rows="4" cols="50">Enter your comments here...</textarea> <br>
        <input type="submit" value="Submit">
    </form>
        

Here, the <textarea> creates a multi-line text box with 4 rows and 50 columns. The user can type comments or other lengthy input.

The <select> Element

The <select> element is used to create a drop-down list. It contains multiple <option> elements, each representing an item in the list.

Example of a <select> Drop-Down:

    <form>
        <label>Choose your country:</label> <br>
        <select name="country">
            <option value="usa">United States</option>
            <option value="canada">Canada</option>
            <option value="uk">United Kingdom</option>
        </select> <br>
        <input type="submit" value="Submit">
    </form>
        

In this example, the <select> element creates a drop-down menu with three options: United States, Canada, and United Kingdom.

Combining All Form Elements

Below is a form that combines all the elements covered in this article:

    <form action="submit_form.php" method="post">
        <label>Username:</label>
        <input type="text" name="username"> <br>

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

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

        <label>Comments:</label> <br>
        <textarea name="comments" rows="4" cols="50">Enter your comments here...</textarea> <br>

        <label>Country:</label> <br>
        <select name="country">
            <option value="usa">United States</option>
            <option value="canada">Canada</option>
            <option value="uk">United Kingdom</option>
        </select> <br>

        <input type="submit" value="Register">
    </form>
        

This form collects a username, password, email, comments, and a country selection. When submitted, it sends the data to submit_form.php using the POST method.

Conclusion

HTML forms are versatile tools for gathering input from users. By using <form> to contain form data, <input> to collect basic inputs, <textarea> for multi-line input, and <select> for drop-down options, you can create a functional form that meets various input needs.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java