HTML
CSS
JavaScript
jQuery
PHP
SQL
WordPress
HTML Tutorials

Overview

An HTML form is used to collect user input and send it to a server for processing.

				
					<form action="forms/processing.php" method="post">
    <label for="first_name">First name:</label>
    <input type="text" id="first_name" name="first_name">

    <label for="last_name">Last name:</label>
    <input type="text" id="last_name" name="last_name">

    <label for="email">Email:</label>
    <input type="email" id="email" name="email">

    <input type="submit" value="Submit">
</form>
				
			

This is what it looks like without any styling.





The first part of the form is the form tag itself, which has an action attribute. The action attribute is usually a PHP document which has the code to process or store the information. The method attribute tells the browser how to send the form content (usually either POST or GET).

GET:

  • Appends the form data to the URL, in name/value pairs
  • The submitted form data is NOT SECURE as it is visible in the URL
  • The length of a URL is limited to 2048 characters
  • Useful for form submissions where a user wants to bookmark the result and return to it later (like product searches with filters)

POST:

  • Appends the form data inside the body of the HTTP request (the form data is not shown in the URL) which makes it more secure
  • POST has no size limitations and can be used to send large amounts of data
  • Form submissions with POST cannot be bookmarked as the data is hidden from the URL bar
				
					<form action="forms/processing.php" method="post">
  
</form>
				
			

Inside the form are several sets of labels and corresponding input fields. The label uses a “for” attribute to specify which input it belongs to (the input has a matching #id).

In the example below we have two text inputs to collect the user’s first and then last name. The third input is an email type to collect the user’s email address.

				
					<label for="first_name">First name:</label>
<input type="text" id="first_name" name="first_name">

<label for="last_name">Last name:</label>
<input type="text" id="last_name" name="last_name">

<label for="email">Email:</label>
<input type="email" id="email" name="email">
				
			

Finally, we have the submit button (another input type) that, when clicked, will send the contents of the form to the PHP document (form action) for processing and often storage in a database. The value is the button text.

				
					<input type="submit" value="Submit">
				
			

Attributes

The following attributes can be used within the form tag.

AttributeValueDescription
accept-charsetcharacter_setSpecifies the character encodings that are to be used for the form submission
actionURLSpecifies where to send the form data when a form is submitted
autocompleteon
off
Specifies whether a form should have autocomplete on or off
enctypeapplication/x-www-form-urlencoded
multipart/form-data
text/plain
Specifies how the form data should be encoded when submitting it to the server (only for method=”post”)
methodget
post
Specifies the HTTP method to use when sending form data
nametextSpecifies the name of a form
novalidatenovalidateSpecifies that the form should not be validated when submitted
relexternal
help
license
next
nofollow
noopener
noreferrer
opener
prev
search
Specifies the relationship between a linked resource and the current document
target_blank
_self
_parent
_top
Specifies where to display the response that is received after submitting the form

The following attributes can be used within the input tags.

AttributeValueDescription
acceptfile_extension
audio/*
video/*
image/*
media_type
Specifies a filter for what file types the user can pick from the file input dialog box (only for type=”file”)
alttextSpecifies an alternate text for images (only for type=”image”)
autocompleteon
off
Specifies whether an <input> element should have autocomplete enabled
autofocusautofocusSpecifies that an <input> element should automatically get focus when the page loads
checkedcheckedSpecifies that an <input> element should be pre-selected when the page loads (for type=”checkbox” or type=”radio”)
dirnameinputname.dirSpecifies that the text direction will be submitted
disableddisabledSpecifies that an <input> element should be disabled
formform_idSpecifies the form the <input> element belongs to
formactionURLSpecifies the URL of the file that will process the input control when the form is submitted (for type=”submit” and type=”image”)
formenctypeapplication/x-www-form-urlencoded
multipart/form-data
text/plain
Specifies how the form-data should be encoded when submitting it to the server (for type=”submit” and type=”image”)
formmethodget
post
Defines the HTTP method for sending data to the action URL (for type=”submit” and type=”image”)
formnovalidateformnovalidateDefines that form elements should not be validated when submitted
formtarget_blank
_self
_parent
_top
framename
Specifies where to display the response that is received after submitting the form (for type=”submit” and type=”image”)
heightpixelsSpecifies the height of an <input> element (only for type=”image”)
listdatalist_idRefers to a <datalist> element that contains pre-defined options for an <input> element
maxnumber
date
Specifies the maximum value for an <input> element
maxlengthnumberSpecifies the maximum number of characters allowed in an <input> element
minnumber
date
Specifies a minimum value for an <input> element
minlengthnumberSpecifies the minimum number of characters required in an <input> element
multiplemultipleSpecifies that a user can enter more than one value in an <input> element
nametextSpecifies the name of an <input> element
patternregexpSpecifies a regular expression that an <input> element’s value is checked against
placeholdertextSpecifies a short hint that describes the expected value of an <input> element
readonlyreadonlySpecifies that an input field is read-only
requiredrequiredSpecifies that an input field must be filled out before submitting the form
sizenumberSpecifies the width, in characters, of an <input> element
srcURLSpecifies the URL of the image to use as a submit button (only for type=”image”)
stepnumber
any
Specifies the interval between legal numbers in an input field
typebutton
checkbox
color
date
datetime-local
email
file
hidden
image
month
number
password
radio
range
reset
search
submit
tel
text
time
url
week
Specifies the type <input> element to display
valuetextSpecifies the value of an <input> element
widthpixelsSpecifies the width of an <input> element (only for type=”image”)

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.

HTML
CSS
JavaScript
jQuery
PHP
SQL
WordPress

Why 1SMARTchicken?

This site was built and is maintained to benefit my autistic son.
See More →

My Son's Name is Johnny

He was diagnosed as autistic quite late, at age four...
His story

Buy Me a Coffee

Thanks for your support!

Feedback

If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.