HTML Tutorials

Overview

To expand on the basic HTML form, we can add text inputs, textarea inputs, checkboxes, and radio buttons to gather user information to be sent to a server for processing.

<form action="forms/processing.php">
    <!-- text and email inputs -->
    <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">


    <!-- radio inputs -->
    <input type="radio" id="html" name="languages" value="HTML">
    <label for="html">HTML</label>

    <input type="radio" id="css" name="languages" value="CSS">
    <label for="css">CSS</label>

    <input type="radio" id="javascript" name="languages" value="JavaScript">
    <label for="javascript">JavaScript</label>


    <!-- checkbox inputs-->
    <input type="checkbox" id="vehicle_1" name="vehicle_1" value="Bike">
    <label for="vehicle_1"> I have a Lamborghini</label>

    <input type="checkbox" id="vehicle_2" name="vehicle_2" value="Car">
    <label for="vehicle_2"> I have a Ferrari</label>

    <input type="checkbox" id="vehicle_3" name="vehicle_3" value="Boat">
    <label for="vehicle_3"> I have a Maserati</label>


    <!-- dropdown input -->
    <label for="cars">Choose a car:</label>
    <select id="cars" name="cars">
        <option value="lamborghini">Lamborghini</option>
        <option value="ferrari">Ferrari</option>
        <option value="maserati" selected>Maserati</option>
    </select>


    <!-- textarea input -->
    <textarea name="message" rows="10" cols="30"></textarea>


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

The code above would give us the unformatted form below.

















After the text and email inputs we have a set of radio button inputs. Radio buttons only allow for one selection out of multiple choices. Just like the text and email inputs, each of the radio inputs has a corresponding label  that uses the name attribute to refer to the #id of the input it belongs to.

Additionally, each radio input has a value attribute with the information that is to be passed when the form is submitted.

<!-- radio inputs -->
<input type="radio" id="html" name="languages" value="HTML">
<label for="html">HTML</label>

<input type="radio" id="css" name="languages" value="CSS">
<label for="css">CSS</label>

<input type="radio" id="javascript" name="languages" value="JavaScript">
<label for="javascript">JavaScript</label>

Next we have a set of HTML checkboxes which allow the user to select zero or more options from a number of choices. Again, each checkbox input has a corresponding label, and like the radio button has a value that will be send with the rest of the form data.

<!-- checkbox inputs-->
<input type="checkbox" id="vehicle_1" name="vehicle_1" value="Bike">
<label for="vehicle_1"> I have a Lamborghini</label>

<input type="checkbox" id="vehicle_2" name="vehicle_2" value="Car">
<label for="vehicle_2"> I have a Ferrari</label>

<input type="checkbox" id="vehicle_3" name="vehicle_3" value="Boat">
<label for="vehicle_3"> I have a Maserati</label>

Next we have a dropdown list (select element) from which the user can make one or more selections. The label for the select applies to the entire dropdown. By default, the first item in the drop-down list is selected, but in the following we have the “Maserati” preselected for the user by using the selected attribute (it has no value; by just appearing it “selects” that option item).

Each option within the <select> element has a value attached which is what is sent when the form is submitted.

<!-- dropdown input -->
<label for="cars">Choose a car:</label>
<select id="cars" name="cars">
    <option value="lamborghini">Lamborghini</option>
    <option value="ferrari">Ferrari</option>
    <option value="maserati" selected>Maserati</option>
</select>

Finally, we have a textarea input which is a text box where the user can write a message to be sent when the form is submitted. The rows and cols attributes specify how big the form should appear on the page (ten rows in height and 30 columns in width).

<!-- textarea input -->
<textarea name="message" rows="10" cols="30"></textarea>

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")

The following attributes can be used within the <select> tag.

AttributeValueDescription
autofocusautofocusSpecifies that the drop-down list should automatically get focus when the page loads
disableddisabledSpecifies that a drop-down list should be disabled
formform_idDefines which form the drop-down list belongs to
multiplemultipleSpecifies that multiple options can be selected at once
namenameDefines a name for the drop-down list
requiredrequiredSpecifies that the user is required to select a value before submitting the form
sizenumberDefines the number of visible options in a drop-down list

The following attributes can be used within the <textarea> tag.

AttributeValueDescription
autofocusautofocusSpecifies that the text area should automatically get focus when the page loads
colsnumberSpecifies the visible width of the text area
dirnametextareaname.dirSpecifies that the text direction of the text area will be submitted
disableddisabledSpecifies that the text area should be disabled
formform_idSpecifies which form the text area belongs to
maxlengthnumberSpecifies the maximum number of characters allowed in the text area
nametextSpecifies a name for the text area
placeholdertextSpecifies a short hint that describes the expected value of the text area
readonlyreadonlySpecifies that a text area should be read only
requiredrequiredSpecifies that the text area is required and must be filled out
rowsnumberSpecifies the visible number of lines in a text area
wraphard
soft
Specifies how the text in a text area is to be wrapped when submitted in a form

HTML Notes:

  • In our HTML section the term “tag” and “element” are often used interchangeably to refer to both the tag used to create a page element and the element created by the tag (<p> tag = <p> element = paragraph on the page)
  • HTML5 is not case sensitive; so <P> is the same as <p>, <H1> is the same as <h1>
  • Global attributes can be used with all HTML tags and are therefore not mentioned on every tag page
  • To write clean, readable HTML code, it is best to use indentation whereas elements within elements are indented (tabbed or spaces) to create something that looks like a project outline
  • The browser will automatically remove any extra spaces and lines in your HTML code when the page is displayed
  • Double quotes or single quotes can be used around HTML attribute values, but when the attribute value itself contains one form of quote, it will be necessary to use the other around the attribute

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.