An HTML form groups controls such as text fields, checkboxes, radio buttons, select boxes, textareas and submit buttons so user-entered data can be sent for processing.
<form action="signupck.php" method="post">
<label for="first_name">First name</label>
<input type="text" id="first_name" name="first_name" required>
<button type="submit">Submit</button>
</form>
The <form> element defines where the form starts and ends. Controls that should be submitted normally need a name attribute.
<form action="file_name.php" method="post">
<!-- Form controls go here -->
</form>
The most important form attributes are:
action: where the submitted data is sent.method: how the browser sends the form data, usually GET or POST.If method is omitted, the default is GET.
Choose the control according to the kind of data the user must enter.
The control should make the expected input obvious and should be paired with a clear label.
The action attribute identifies the URL that receives the submitted form data.
<form action="signupck.php" method="post">
...
</form>
The action can be a relative URL:
<form action="../process/signupck.php" method="post">
or an absolute URL when the form is intentionally submitted to another origin:
<form action="https://www.example.com/process.php" method="post">
If the action is omitted, the form submits to the current document URL.
The two common form methods are get and post.
| Method | Typical use | Where data appears |
|---|---|---|
GET | Searches, filters and requests that do not change server data | URL query string |
POST | Submitting data that creates or changes server-side state | HTTP request body |
See GET and POST methods in PHP.
GET is useful when the request represents a retrievable resource or view, such as a search or filter.
<form action="search.php" method="get">
<label for="q">Search</label>
<input type="text" id="q" name="q">
<button type="submit">Search</button>
</form>
A submitted value might produce a URL such as:
search.php?q=mysql
Because the parameters are in the URL, GET requests can often be bookmarked and shared.
POST is commonly used for login forms, registration forms, uploads and requests that create or modify server-side data.
<form action="signupck.php" method="post">
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<button type="submit">Create account</button>
</form>
The values are sent in the request body rather than appended to the URL.
name identifies the submitted field. id identifies an element in the document and is commonly used to connect a label to its control.
<label for="city">City</label>
<input type="text" id="city" name="city">
When submitted, the field name becomes part of the name/value pair:
city=Hyderabad
name is generally not included in normal form submission. The id and name may use the same value, but they serve different purposes.Associate visible text with each form control by using <label>.
<label for="full_name">Full name</label>
<input type="text" id="full_name" name="full_name">
The label improves usability for keyboard, screen-reader and pointer users. Clicking the label also activates many associated controls.
A placeholder is not a replacement for a visible label because placeholder text can disappear after the user starts typing.
Use <fieldset> and <legend> for a related group such as radio buttons or checkboxes.
<fieldset>
<legend>Preferred contact method</legend>
<input type="radio" id="contact_email" name="contact" value="email">
<label for="contact_email">Email</label>
<input type="radio" id="contact_phone" name="contact" value="phone">
<label for="contact_phone">Phone</label>
</fieldset>
Radio buttons that belong to one mutually exclusive group use the same name.
HTML provides attributes that help browsers check common input requirements before submission.
<label for="email">Email</label>
<input
type="email"
id="email"
name="email"
maxlength="120"
autocomplete="email"
required
>
Common validation-related attributes include required, min, max, minlength, maxlength and pattern.
A form that uploads a file must use POST and multipart/form-data:
<form action="uploadck.php" method="post" enctype="multipart/form-data">
<label for="document">Choose a file</label>
<input type="file" id="document" name="document">
<button type="submit">Upload</button>
</form>
See HTML file upload fields and PHP file upload handling.
| Element | Use |
|---|---|
| Text field | Short text such as a name or address |
| Hidden field | Submit a value that is not presented as an editable control |
| Password field | Mask characters while the user types a password |
| Textarea | Multi-line text |
| Radio button | Select one option from a mutually exclusive group |
| Checkbox | Select independent yes/no options or multiple choices |
| Select / drop-down list | Select one or more predefined options |
| Optgroup | Group related options inside a select element |
| Datalist | Offer suggestions while still allowing other text values |
| Button | Trigger an action or script |
| Image button | Submit a form using an image control |
| Submit button | Submit the form |
| Reset button | Restore form controls to their initial values |
| Tutorial | Purpose |
|---|---|
| Button as hyperlink | Navigate using a button-style link |
| Image map | Link different areas of an image |
| Hyperlink | Create normal HTML links |
| Link window | Open a link in another browsing context |
| Parent and child windows | Work with JavaScript-created windows |
| Links within a page | Jump to sections such as FAQ answers |
| Image area | Create links on areas of an image map |
| Email link | Create a mailto link |
| Application | Tutorial |
|---|---|
| Set focus on a field | Set Focus |
| Use two submit buttons | Two Submit Buttons |
| Submit to another browsing context | Submit to New Window |
| Return to a previous page | History Back |
| Upload a file | File Upload |
| Compare selection controls | Select Options |
Create a signup form that asks for:
Each control should have a visible label. Submit the form from signup.html to signupck.php.
Continue with collecting HTML form data in PHP.
For a GET-based exercise, see using query-string data as a textbox default and query-string data formatting.
It groups form controls and defines how and where their submitted values are sent.
GET places submitted parameters in the URL and is useful for retrievable requests such as searches. POST sends data in the request body and is commonly used for requests that create or change server-side state.
POST keeps values out of the URL but does not encrypt them. Use HTTPS and secure server-side processing for sensitive data.
The name identifies the submitted field. A normal successful control without a name is generally not included in form submission.
The name is used for submitted form data. The id identifies the HTML element and can connect it to a label through the label's for attribute.
No. Browser validation improves usability but can be bypassed. The server must validate submitted values again.
Use enctype="multipart/form-data" with method="post".
Next: Text field in an HTML form
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| jaki shahrier kanak | 20-03-2010 |
| More eaxmples are needed | |