loading

HTML Forms

Html, Html5, Html Tutorials, Learn Html, Free Html Tutorials, Html Example, Html Explained, Html Forms, User Input, Form Element, Input Element, Text Field, Label Element, Radio Button, Checkbox, Submit Button, Name Attribute

To gather user input, an HTML forms is utilized. Most of the time, a server receives user input and processes it.


Example






The < form > Element

To build an HTML form for user input, use the < form > element in HTML:

				
					<form>
.
form elements
.
</form>
				
			

A variety of input components, including text fields, checkboxes, radio buttons, submit buttons, and more, can be contained within the <form> element.

The < input > Element

The HTML <input> element is the most used form element.

An <input> element can be displayed in many ways, depending on the type attribute.

Here are some examples:

Type Description
<input type="text"> Displays a single-line text input field
<input type="radio"> Displays a radio button (for selecting one of many choices)
<input type="checkbox"> Displays a checkbox (for selecting zero or more of many choices)
<input type="submit"> Displays a submit button (for submitting the form)
<input type="button"> Displays a clickable button

Text Fields

The <input type=”text”> defines a single-line input field for text input.

Example

A form with input fields for text:

				
					<form>
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname">
</form>

				
			

This is how the HTML code above will be displayed in a browser:

First name:

Last name:

The < label > Element

Take note of how the <label> element is used in the previous example.

For many form elements, a label is defined using the <label> tag.

Users of screen readers will find the <label> element helpful since it will read the label aloud when the user concentrates on the input element.

Because it toggles the radio button or checkbox when the user clicks within it, the <label> element also aids users who have trouble clicking on small regions, like radio buttons or checkboxes.

To link them together, the id attribute of the <input> element and the for attribute of the <label> tag must be equivalent.

Radio Buttons

The <input type=”radio”> defines a radio button.

Radio buttons let a user select ONE of a limited number of choices.

Example

A form with radio buttons:

				
					<p>Choose your favorite Web language:</p>

<form>
  <input type="radio" id="html" name="fav_language" value="HTML">
  <label for="html">HTML</label><br>
  <input type="radio" id="css" name="fav_language" value="CSS">
  <label for="css">CSS</label><br>
  <input type="radio" id="javascript" name="fav_language" value="JavaScript">
  <label for="javascript">JavaScript</label>
</form>
				
			

The HTML code above will appear in a browser like this:

Select your preferred web language:




Checkboxes

The <input type=”checkbox”> defines a checkbox.

Checkboxes let a user select ZERO or MORE options of a limited number of choices.

Example

A form with checkboxes:

				
					<form>
  <input type="checkbox" id="vehicle1" name="vehicle1" value="Bicycle">
  <label for="vehicle1"> I have a bicycle</label><br>
  <input type="checkbox" id="vehicle2" name="vehicle2" value="Bus">
  <label for="vehicle2"> I have a bus</label><br>
  <input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
  <label for="vehicle3"> I have a boat</label>
</form>

				
			

This is how the HTML code above will be displayed in a browser:



The Submit Button

The button to submit form data to a form-handler is defined by the <input type=”submit”>.

Usually, the form-handler is a server file that contains a script to process incoming data.

The action attribute of the form specifies the form-handler.

Example

A form with a submit button:

				
					<form action="/action_page.php">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="Johnny"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Doesun"><br><br>
  <input type="submit" value="Submit">
</form>

				
			

This is how the HTML code above will be displayed in a browser:

First name:

Last name:


The Name Attribute for < input >

Observe that in order for an input field to be submitted, a name attribute is required.

The input field’s value won’t be delivered at all if the name attribute is left off.

Example

This example will not submit the value of the “First name” input field:

				
					<form action="/action_page.php">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" value="Johnny"><br><br>
  <input type="submit" value="Submit">
</form>

				
			

HTML

HTML5

HTML tutorials

Learn HTML

Free HTML tutorials

HTML Example

HTML Explained

HTML forms

user input

form element

input element

text field

label element

radio button
checkbox
submit button
name attribute
Share this Doc

HTML Forms

Or copy link

Explore Topic