Forms in HTML
? Forms in HTML
Forms allow users to input data and submit it to a server or process it on the client side.
Basic Structure of a Form
<form action="/submit" method="POST"> <!-- Form elements go here --></form>action: URL where form data is sentmethod: HTTP method (GETorPOST)
Common Form Elements
| Element | Description | Example |
|---|---|---|
<input> | Various types: text, password, checkbox, radio, etc. | <input type="text" name="username"> |
<textarea> | Multi-line text input | <textarea name="message"></textarea> |
<select> | Drop-down list | <select name="color"><option>Red</option></select> |
<option> | Options inside <select> | See above |
<button> | Button element (submit/reset) | <button type="submit">Send</button> |
<label> | Label for inputs (improves accessibility) | <label for="email">Email:</label> |
Example Form
<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="email" id="email" name="email" required> <label>Gender:</label> <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> <label for="color">Favorite Color:</label> <select id="color" name="color"> <option value="red">Red</option> <option value="blue">Blue</option> </select> <button type="submit">Submit</button></form>Want help with form validation, styling forms with CSS, or handling form data in JavaScript?