Contents
HTML form: Main Tips
- The HTML
<form>
tag groups elements and sends their data to a web server. - A typical form element includes an
<input>
element with atype
value ofsubmit
, which is used to submit an HTML<form>
.
Use of form Element
The HTML <form>
defines an area, containing interactive elements for submitting data.
<form action="search" method="GET">
Search Term: <input type="text" name="search_query">
<input type="submit" value="Search">
</form>
- The HTML elements <input>, <textarea>, <button> and <select> with <option> collect data in a form.
- Also, <fieldset> groups
<form>
elements. <label> provides titles for<input>
elements. - Other element is <optgroup> for grouping
<option>
elements inside the<select>
element.
Attributes to Use With form
accept
Sets a comma-separated list of types that a server accepts.
<form action="upload" method="POST" enctype="multipart/form-data" accept="image/jpeg, image/png">
Select Image: <input type="file" name="uploaded_filename">
<input type="submit" value="Upload">
</form>
Note: removed in HTML5. Apply the accept attribute for a specific <input> element as an alternative.
accept-charset
Sets a space-separated list of character encodings used while submitting data.
<form action="search" method="GET" accept-charset="ANSI ISO-8859-1">
Search Term: <input type="text" name="search_query">
<input type="submit" value="Search">
</form>
action
The URI of a program, processing the <form>
data.
<form action="http://www.bitdegree.org/" method="GET">
Search Term: <input type="text" name="search_query">
<input type="submit" value="Search">
</form>
autocomplete
Sets the autocomplete feature to ON or OFF.
<form action="register" method="POST" autocomplete="on">
Surname: <input type="text" name="name">
Personal mail: <input type="text" name="email">
<input type="submit" value="Register">
</form>
enctype
In cases when method
attribute is post
, enctype
becomes the MIME type of data used for sending the form to the server.
<form action="upload" method="POST" enctype="multipart/form-data" accept="image/*">
Select Image: <input type="file" name="uploaded_filename">
<input type="submit" value="Upload">
</form>
method
Sets the type of HTTP method for form submission.
<form action="search" method="GET">
Search Term: <input type="text" name="search_query">
<input type="submit" value="Search">
</form>
name
Defines the <form>
name.
<form name="searchForm" action="search" method="GET">
Search Term: <input type="text" name="search_query">
<input type="submit" value="Search">
</form>
Note: HTML4 deprecated the name attribute. ID applies instead. In HTML5, the attribute cannot be empty and must be unique.
novalidate
Sets a form not to be validated during submission.
<form action="register" method="POST" novalidate>
Surname: <input type="text" name="name">
Personal mail: <input type="text" name="email" required>
<input type="submit" value="Register">
</form>
target
Sets the target location for the web server response.
<form action="http://www.bitdegree.org/" method="GET" target="_blank">
Search Term: <input type="text" name="search_query">
<input type="submit" value="Search">
</form>