HTML Forms

What are HTML Forms?

HTML forms are used to collect user input. The <form> element is a container for different types of input elements, such as text fields, checkboxes, radio buttons, submit buttons, etc.

Basic Form

Example
<form>
    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    <br><br>
    <input type="submit" value="Submit">
</form>

Input Types

Text Input

Example
<input type="text" name="username" placeholder="Enter username">

Password Input

Example
<input type="password" name="password">

Email Input

Example
<input type="email" name="email">

Checkbox

Example
<input type="checkbox" id="agree" name="agree">
<label for="agree">I agree to the terms</label>

Radio Buttons

Example
<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>

Select Dropdown

Example
<select name="country">
    <option value="usa">USA</option>
    <option value="uk">UK</option>
    <option value="canada">Canada</option>
</select>

Textarea

Example
<textarea name="message" rows="4" cols="50">Enter your message</textarea>

Form Attributes

Complete Form Example

Example
<form action="/submit" method="POST">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required>
    <br><br>
    
    <label for="email">Email:</label>
    <input type="email" id="email" name="email" required>
    <br><br>
    
    <label for="message">Message:</label>
    <textarea id="message" name="message" rows="4"></textarea>
    <br><br>
    
    <input type="submit" value="Submit">
</form>