HTML Forms
HTML Forms Tutorial by CodesWithPankaj
HTML forms are essential for collecting user input. They provide a way for users to submit data that gets processed, often on the server side. Let's explore the various tags and form elements available to create an interactive and functional form.
1. <form>
Tag – Form Container
<form>
Tag – Form ContainerThe <form>
tag is the main container for all form elements. It defines the area where user input is collected and specifies how to send the data.
Important Attributes:
action
: Specifies the URL to send form data when the form is submitted.method
: Specifies the HTTP method (GET
orPOST
) to use for sending data.enctype
: Specifies the encoding type of the data. Commonly used for file uploads (multipart/form-data
).target
: Defines where to display the response (e.g.,_blank
for a new tab).
Example:
2. <input>
Tag – Input Field
<input>
Tag – Input FieldThe <input>
tag is a versatile form element used to create various types of input fields. The type
attribute determines the kind of input field it will create.
Common Types:
type="text"
– Single-line text input.type="password"
– Hidden input for passwords.type="email"
– Email input with validation.type="number"
– Number input with a spinner.type="date"
– Date input with a date picker.type="checkbox"
– Checkbox for selecting multiple options.type="radio"
– Radio button for selecting one option from a group.type="submit"
– Submit button to send form data.type="reset"
– Reset button to clear all inputs.type="file"
– File upload input.
Example:
Additional Attributes:
placeholder
: Shows hint text inside the input.value
: Sets a default value for the input.name
: Assigns a name for the input (important for form data).required
: Marks the field as mandatory.readonly
: Makes the field non-editable.disabled
: Disables the input.
3. <textarea>
Tag – Multi-line Text Input
<textarea>
Tag – Multi-line Text InputThe <textarea>
tag creates a multi-line text box, useful for longer input, such as comments or descriptions.
Example:
Attributes:
rows
: Specifies the number of visible text lines.cols
: Defines the width of the text box.placeholder
: Displays a hint text.
4. <select>
Tag – Dropdown List
<select>
Tag – Dropdown ListThe <select>
tag is used to create a dropdown list, where users can select one or multiple options.
Example:
Attributes:
multiple
: Allows multiple selections (useCtrl
key to select multiple items).size
: Defines the number of visible options.
5. <option>
Tag – Option in a Dropdown List
<option>
Tag – Option in a Dropdown ListThe <option>
tag defines each item in a <select>
dropdown list. Each option has a value
attribute that is submitted with the form.
Example:
Attributes:
value
: Specifies the value submitted for that option.selected
: Pre-selects the option by default.
6. <button>
Tag – Button
<button>
Tag – ButtonThe <button>
tag creates a clickable button. Unlike <input type="submit">
, <button>
can contain text or HTML.
Example:
Types:
type="submit"
: Submits the form.type="reset"
: Resets all fields.type="button"
: A regular button with no default action.
7. <label>
Tag – Input Label
<label>
Tag – Input LabelThe <label>
tag provides a label for form elements, improving accessibility and usability. The for
attribute links the label to an input’s id
.
Example:
8. <fieldset>
and <legend>
Tags – Grouping Form Elements
<fieldset>
and <legend>
Tags – Grouping Form ElementsThe <fieldset>
tag is used to group related elements, often with a <legend>
tag that provides a caption for the field group.
Example:
9. <datalist>
Tag – Predefined Options for Input
<datalist>
Tag – Predefined Options for InputThe <datalist>
tag provides a list of predefined options for an <input>
element, offering suggestions as the user types.
Example:
10. <output>
Tag – Display Calculation or Result
<output>
Tag – Display Calculation or ResultThe <output>
tag is used to display the result of a calculation or an operation performed by JavaScript.
Example:
Full Example of HTML Form with All Tags
Here’s a complete example that includes all the various form elements discussed above:
Summary
HTML forms are vital for gathering user input in web applications. By understanding the different tags and attributes, you can create functional and user-friendly forms. Practice using these elements to enhance your web development skills! Enjoy creating with CodesWithPankaj!
Last updated