Basic Structure of an HTML Form
A form is created using the <form>
element. Inside it, various form controls are added to collect data. The data is submitted to the server through the action specified in the action
attribute, and the method used for submission is specified in the method
attribute.
Example:
action
: The URL where the form data will be submitted.method
: The HTTP method used for sending data (GET
orPOST
).
Common Form Elements
Here are some of the most commonly used form elements:
1. Text Input (<input type="text">
)
Used for single-line text input.
2. Password Field (<input type="password">
)
Used for entering passwords. The characters are hidden as they are typed.
3. Email Input (<input type="email">
)
Used for entering an email address. It will validate the input to ensure it is in the correct format.
4. Submit Button (<input type="submit">
)
A button used to submit the form data to the server.
5. Radio Buttons (<input type="radio">
)
Used when you want the user to select one option from a set of predefined options.
6. Checkboxes (<input type="checkbox">
)
Used for selecting multiple options from a set.
7. Dropdown List (<select>
)
Creates a dropdown list where the user can select one or more options.
8. Textarea (<textarea>
)
Used for multi-line text input.
9. Date Input (<input type="date">
)
Allows the user to select a date from a date picker.
10. Number Input (<input type="number">
)
Allows the user to enter a numeric value. You can also set a range using min
and max
.
11. File Input (<input type="file">
)
Allows the user to upload a file.
12. Hidden Input (<input type="hidden">
)
Hidden inputs are used to include data in a form that users cannot see or change.
Attributes Used in Forms
Here are some common attributes used with form elements:
name
: The name of the form control. It's used as the key when sending form data to the server.value
: The value associated with the form element, often used with checkboxes, radio buttons, and submit buttons.required
: Ensures that the user cannot submit the form without filling in this field.placeholder
: Provides a hint to the user about what to input.disabled
: Disables a form control so that it cannot be interacted with.readonly
: Makes a field read-only, meaning the user cannot change the value.min
/max
: Defines the minimum and maximum values for input elements such as numbers or dates.
Example: A Complete Form
This example contains various input types commonly used in forms.
Methods: GET
vs. POST
GET
: Appends form data to the URL as query strings. Use it for simple data submission or searches (data is visible in the URL).
No comments:
Post a Comment