HTML Forms:
HTML forms are crucial for user interaction on the web. They enable users to submit data to a server for processing. Let's explore the various components and attributes associated with HTML forms.
- Basic Form Structure:
- The <form> element is used to define an HTML form. It encloses all the form elements.
- action: Specifies the URL where the form data will be sent.
- method: Defines the HTTP method for sending form data, typically "get" or "post."
- Text Input:
- The <input> element with the type="text" attribute creates a single-line text input field.
- id: Unique identifier for the input.
- name: Name of the input, used when submitting the form.
- Password Input:
- The <input> element with type="password" creates a password input field.
- Email Input :
- The email input enforces the entry of a valid email address.
- Radio Buttons:
- Radio buttons are created using the <input> element with type="radio". They are often used in groups where users can select only one option.
- name: Should be the same for radio buttons in the same group.
- Checkboxes:
- Checkboxes are created using the <input> element with type="checkbox". Users can select multiple options.
- Dropdown Lists:
- The <select> element is used to create a dropdown list, and <option> elements define the options within the list.
- Textarea:
- The <textarea> element creates a multiline text input field.
- rows and cols: Define the number of rows and columns for the textarea.
- Submit Button:
- The <input> element with type="submit" creates a button that submits the form.
- Reset Button:
- The <input> element with type="reset" creates a button that resets the form fields to their default values.
- Form Attributes: Additional attributes for the <form> element:
- action: Specifies the URL where the form data will be sent.
- method: Defines the HTTP method for sending form data, typically "get" or "post."
- target: Specifies where to open the response from the server (e.g., in a new window).
<form action="/submit" method="post">
<!-- Form elements go here -->
</form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
Output:-
<label for="password">Password:</label>
<input type="password" id="password" name="password">
Output:-
<tlabel for="email">Email:</label>
<input type="email" id="email" name="email">
Output
<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>
Output:-
<input type="checkbox" id="subscribe" name="subscribe" value="yes">
<label for="subscribe">Subscribe to newsletter</label>
Output:-
<label for="course">Course:</label>
<select id="course" name="course">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="js">JavaScript</option>
</select>
Output:-
<label for="message">Message:</label>
<textarea id="message" name="message" rows="3" cols="40"> </textarea>
Output:-
<input type="submit" value="Submit">Output:-
<input type="reset" value="Reset">
Output:-
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HTML Form Example</title> </head> <body> <h1>Contact Us</h1> <form action="/submit" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <label for="email">Email:</label> <input type="text" id="email" name="email" required> <label for="message">Message:</label> <textarea id="message" name="message" rows="4" cols="50" required></textarea> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form> </body> </html>Output