Key HTML Form Elements
The <form>
Element
The <form>
element is the container for all other form elements. It defines the boundaries of the form and specifies how the collected data should be submitted. Crucially, it contains the action
and method
attributes.
action
: Specifies the URL where the form data will be sent when the form is submitted. This is typically a server-side script that will handle the data. Example: action="/submit-form.php"
method
: Specifies the HTTP method used to send the form data. The two most common methods are: GET
: Appends the form data to the URL (visible in the address bar). Suitable for small amounts of non-sensitive data. Limited in the amount of data it can transmit. POST
: Sends the form data in the HTTP request body (not visible in the address bar). Suitable for larger amounts of data and sensitive information. The preferred method for most forms.
Example:<form action="/submit-form.php" method="POST"> <!-- Form elements go here --> </form>
The <input>
Element
The <input>
element is the most versatile form element. It's used to create various types of input fields, depending on the type
attribute.
Common type
Attributes:
text
: A single-line text input field. <input type="text" name="username">
password
: A password input field (characters are masked). <input type="password" name="password">
email
: An email input field (often with built-in validation). <input type="email" name="email">
number
: A numeric input field (may allow specifying minimum and maximum values). <input type="number" name="age" min="0" max="120">
date
: A date input field. <input type="date" name="birthdate">
radio
: A radio button (used for selecting one option from a group). Requires the same name
attribute for each button in the group. <input type="radio" name="gender" value="male"> Male <input type="radio" name="gender" value="female"> Female
checkbox
: A checkbox (used for selecting multiple options from a list). <input type="checkbox" name="subscribe" value="yes"> Subscribe to newsletter
file
: A file upload field. <input type="file" name="avatar">
hidden
: A hidden field (not displayed to the user, but can store data). <input type="hidden" name="user_id" value="123">
submit
: A submit button (submits the form). <input type="submit" value="Submit">
Can also use a <button>
element with type="submit"
. reset
: A reset button (clears the form fields). <input type="reset" value="Reset">
Can also use a <button>
element with type="reset"
.
Important <input>
Attributes:
name
: A unique name for the input field. This is essential for identifying the data on the server-side. value
: The initial value of the input field (or the value associated with a radio button or checkbox). placeholder
: A hint displayed inside the input field when it's empty. <input type="text" name="search" placeholder="Enter search term...">
required
: Specifies that the input field must be filled out before submitting the form. <input type="text" name="name" required>
id
: A unique identifier for the input field (used for styling with CSS and accessing with JavaScript). Often paired with the <label>
element.
The <textarea>
Element
The <textarea>
element is used to create a multi-line text input field. Suitable for comments, descriptions, or any other long-form text.
Example:<textarea name="message" rows="4" cols="50">Enter your message here...</textarea>
rows
: Specifies the visible number of text lines for the textarea. cols
: Specifies the visible width of the textarea. name
: Crucially, the name attribute allows access to the field value in server-side code.
The <select>
Element
The <select>
element creates a dropdown list of options. The <option>
element defines each option in the list.
Example: <select name="country">
<option value="usa">United States</option>
<option value="canada">Canada</option>
<option value="uk">United Kingdom</option>
</select>
name
: The name attribute for the select element. value
: The value attribute of the option element determines the value that will be submitted when that option is selected. selected
: An attribute on the <option>
element that makes it the default selected option.
The <button>
Element
The <button>
element is used to create clickable buttons. While <input type="submit">
and <input type="reset">
can be used, the <button>
element offers more flexibility in terms of content (you can include HTML elements inside it).
Example:<button type="submit">Submit Form</button>
type="submit"
: Submits the form. type="reset"
: Resets the form. type="button"
: A generic button (usually used with JavaScript).