HTML Forms: The Element
HTML Forms: The Element
HTML Forms
❮ Previous Next ❯
Submit
Try it Yourself »
<form>
.
form elements
.
</form>
Form elements are different types of input elements, like text fields, checkboxes, radio
buttons, submit buttons, and more.
1/9
7/10/2019 HTML Forms
The <input> element can be displayed in several ways, depending on the type attribute.
Type Description
<input type="radio"> Defines a radio button (for selecting one of many choices)
You will learn a lot more about input types later in this tutorial.
Text Input
<input type="text"> defines a one-line input field for text input:
Example
<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it Yourself »
First name:
2/9
7/10/2019 HTML Forms
Last name:
Note: The form itself is not visible. Also note that the default width of a text field is 20
characters.
Example
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
Try it Yourself »
Male
Female
Other
The form-handler is typically a server page with a script for processing input data.
3/9
7/10/2019 HTML Forms
Example
<form action="/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>
Try it Yourself »
First name:
Mickey
Last name:
Mouse
Submit
Normally, the form data is sent to a web page on the server when the user clicks on the
submit button.
In the example above, the form data is sent to a page on the server called
"/action_page.php". This page contains a server-side script that handles the form data:
<form action="/action_page.php">
If the action attribute is omitted, the action is set to the current page.
4/9
7/10/2019 HTML Forms
The default value is " _self " which means the form will be submitted in the current window.
To make the form result open in a new browser tab, use the value " _blank ":
Example
Try it Yourself »
Other legal values are " _parent ", " _top ", or a name representing the name of an iframe.
Example
Try it Yourself »
or:
Example
5/9
7/10/2019 HTML Forms
Try it Yourself »
However, when GET is used, the submitted form data will be visible in the page address
field:
/action_page.php?firstname=Mickey&lastname=Mouse
Notes on GET:
Notes on POST:
POST has no size limitations, and can be used to send large amounts of data.
Form submissions with POST cannot be bookmarked
If the name attribute is omitted, the data of that input field will not be sent at all.
This example will only submit the "Last name" input field:
6/9
7/10/2019 HTML Forms
Example
<form action="/action_page.php">
First name:<br>
<input type="text" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>
Try it Yourself »
Example
<form action="/action_page.php">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>
Try it Yourself »
7/9
7/10/2019 HTML Forms
Personal information:
First name:
Mickey
Last name:
Mouse
Submit
HTML Exercises
Exercise:
In the form below, add an input field with the type "button" and the value "OK".
<form>
< >
</form>
Submit Answer »
Attribute Description
accept- Specifies the charset used in the submitted form (default: the page
charset charset).
action Specifies an address (url) where to submit the form (default: the
8/9
7/10/2019 HTML Forms
submitting page).
autocomplete Specifies if the browser should autocomplete the form (default: on).
method Specifies the HTTP method used when submitting the form (default:
GET).
name Specifies a name used to identify the form (for DOM usage:
document.forms.name).
novalidate Specifies that the browser should not validate the form.
target Specifies the target of the address in the action attribute (default:
_self).
You will learn more about the form attributes in the next chapters.
❮ Previous Next ❯
9/9