Careeroza
Form Basics
The form Element
An HTML form collects user input, which is usually sent to a server for processing. The <form> element is a container for input elements: text fields, checkboxes, radio buttons, submit buttons and more.
<form action="/signup" method="post">
<!-- form elements -->
</form>A Simple Form
Example
Each input has a <label> linked to it with for/id. Clicking the label focuses the field:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
body { font-family: Arial, sans-serif; }
label { display: block; margin-top: 8px; }
input, select, textarea { padding: 6px; margin-top: 4px; }
button, input[type=submit] { margin-top: 10px; padding: 6px 14px; }
</style>
</head>
<body>
<form action="/action_page" method="get">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname" value="Asha">
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname" value="Rao">
<input type="submit" value="Submit">
</form>
</body>
</html>Result
The name, action and method Attributes
| Attribute | Meaning |
|---|---|
name (on inputs) | the key the value is sent under. An input without a name is not submitted. |
action | the URL that receives the form data |
method="get" | data is appended to the URL (?fname=Asha&lname=Rao): for searches and filters |
method="post" | data is sent in the request body: for logins, sign-ups and anything that changes data |
Note: Never use GET for passwords or personal data: the values end up in the URL, browser history and server logs.