HTML Forms

The <form> Element

The HTML <form> element defines a form that is used to collect user input:

<form>
.
form elements
.
</form>

An HTML form contains form elements.

 

Form elements are different types of input elements, like: text fields, checkboxes, radio buttons, submit buttons, and more.

<!DOCTYPE html>

<html>

<body>

 

<h2>HTML Forms</h2>

 

<form action=”/action_page.php”>

  <label for=”fname”>First name:</label><br>

  <input type=”text” id=”fname” name=”fname” value=”John”><br>

  <label for=”lname”>Last name:</label><br>

  <input type=”text” id=”lname” name=”lname” value=”Doe”><br><br>

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

</form> 

 

<p>If you click the “Submit” button, the form-data will be sent to a page called “/action_page.php”.</p>

 

</body>

</html>

HTML Forms






If you click the “Submit” button, the form-data will be sent to a page called “/action_page.php”.

 The <input> Element

The <input> element is the most important form element.

The <input> element is displayed in several ways, depending on the type attribute.

Here are some examples:

a

b

b

b

b

b

b

b

 

 

Text Fields

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

A form with two text input fields:

<!DOCTYPE html>

<html>

<body>

 

<h2>The method Attribute</h2>

 

<p>This form will be submitted using the POST method:</p>

 

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

 

<p>After you submit, notice that, unlike the GET method, the form values is NOT visible in the address bar of the new browser tab.</p>

 

</body>

</html>

The method Attribute

This form will be submitted using the POST method:






After you submit, notice that, unlike the GET method, the form values is NOT visible in the address bar of the new browser tab.

The <label> Element

Notice the use of the <label> element in the example above.

The <label> tag defines a label for many form elements.

The <label> element is useful for screen-reader users, because the screen-reader will read out load the label when the user is focused on the input element.

The <label> element also help users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) – because when the user clicks the text within the <label> element, it toggles the radio button/checkbox.

The for attribute of the <label> tag should be equal to the id attribute of the <input> element to bind them together.


Radio Buttons

<input type="radio"> defines a radio button.

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

<!DOCTYPE html>

<html>

<body>

 

<h2>Radio Buttons</h2>

 

<form>
  <input type=”radio” id=”male” name=”gender” value=”male”>
  <label for=”male”>Male</label><br>
  <input type=”radio” id=”female” name=”gender” value=”female”>
  <label for=”female”>Female</label><br>
  <input type=”radio” id=”other” name=”gender” value=”other”>
  <label for=”other”>Other</label>
</form>

 

</body>

</html>

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

 
 
 

The Submit Button

<input type="submit"> defines a button for submitting the form data to a form-handler.

The form-handler is typically a page on the server with a script for processing input data.

The form-handler is specified in the form’s action attribute.

<!DOCTYPE html>

<html>

<body>

 

<h2>HTML Forms</h2>

 

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

 

<p>If you click the “Submit” button, the form-data will be sent to a page called “/action_page.php”.</p>

 

</body>

</html>

HTML Forms






If you click the “Submit” button, the form-data will be sent to a page called “/action_page.php”.

The Action Attribute

The action attribute defines the action to be performed when the form is submitted.

Usually, the form data is sent to a 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.

 

The Target Attribute

The target attribute specifies if the submitted result will open in a new browser tab, a frame, or in the current window.

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“.

<form action=”/action_page.php” target=”_blank”>

<!DOCTYPE html>

<html>

<body>

 

<h2>The form target attribute</h2>

 

<p>When submitting this form, the result will be opened in a new browser tab:</p>

 

<form action=”/action_page.php” target=”_blank”>

  <label for=”fname”>First name:</label><br>

  <input type=”text” id=”fname” name=”fname” value=”John”><br>

  <label for=”lname”>Last name:</label><br>

  <input type=”text” id=”lname” name=”lname” value=”Doe”><br><br>

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

</form> 

 

</body>

</html>

The form target attribute

When submitting this form, the result will be opened in a new browser tab:






Other legal values are “_parent“, “_top“, or a name representing the name of an iframe.

The Method Attribute

The method attribute specifies the HTTP method (GET or POST) to be used when submitting the form data.

Use the GET method when submitting the form:

 

<!DOCTYPE html>

<html>

<body>

 

<h2>The method Attribute</h2>

 

<p>This form will be submitted using the GET method:</p>

 

<form action=”/action_page.php” target=”_blank” method=”get”>

  <label for=”fname”>First name:</label><br>

  <input type=”text” id=”fname” name=”fname” value=”John”><br>

  <label for=”lname”>Last name:</label><br>

  <input type=”text” id=”lname” name=”lname” value=”Doe”><br><br>

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

</form>

 

<p>After you submit, notice that the form values is visible in the address bar of the new browser tab.</p>

 

</body>

</html>

The method Attribute

This form will be submitted using the GET method:






After you submit, notice that the form values is visible in the address bar of the new browser tab.

Use the POST method when submitting the form:

 

<!DOCTYPE html>

<html>

<body>

 

<h2>The method Attribute</h2>

 

<p>This form will be submitted using the POST method:</p>

 

<form action=”/action_page.php” target=”_blank” method=”post”>

  <label for=”fname”>First name:</label><br>

  <input type=”text” id=”fname” name=”fname” value=”John”><br>

  <label for=”lname”>Last name:</label><br>

  <input type=”text” id=”lname” name=”lname” value=”Doe”><br><br>

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

</form>

 

<p>After you submit, notice that, unlike the GET method, the form values is NOT visible in the address bar of the new browser tab.</p>

 

</body>

</html>

The method Attribute

This form will be submitted using the POST method:






After you submit, notice that, unlike the GET method, the form values is NOT visible in the address bar of the new browser tab.

When to Use GET?

The default HTTP method when submitting form data is GET.

However, when GET is used, the form data will be visible in the page’s address field:

/action_page.php?firstname=John&lastname=Doe

Notes on GET:

  • Appends form-data into the URL in name/value pairs
  • The length of a URL is limited (2048 characters)
  • Never use GET to send sensitive data! (will be visible in the URL)
  • Useful for form submissions where a user wants to bookmark the result
  • GET is better for non-secure data, like query strings in Google

When to Use POST?

Always use POST if the form data contains sensitive or personal information. The POST method does not display the form data in the page address field.

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

The Name Attribute

Each input field must have a name attribute to be submitted.

If the name attribute is omitted, the data of that input field will not be sent at all.

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

 

<!DOCTYPE html>

<html>

<body>

 

<h2>The name Attribute</h2>

 

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

 

<p>If you click the “Submit” button, the form-data will be sent to a page called “/action_page.php”.</p>

 

<p>Notice that the value of the “First name” field will not be submitted, because the input element does not have a name attribute.</p>

 

</body>

</html>

The name Attribute




If you click the “Submit” button, the form-data will be sent to a page called “/action_page.php”.

Notice that the value of the “First name” field will not be submitted, because the input element does not have a name attribute.