6. HTML Forms

Key Terms

  • Form
  • Input
  • Text
  • Radio Buttons
  • Checkboxes
  • Submit
  • Select Lists
  • Textarea
  • Form Action
  • Form Method
HTML Forms

HTML enables us to create forms. This is where our websites can become more than just a nice advertising brochure. Forms allow us to build more dynamic websites.

A HTML form is made up of any number of form elements. These elements enable the user to do things such as enter information or make a selection from preset options.

In HTML, a form is defined using the <form> opening tag and the </form> closing tag. The actual form elements are defined between these two tags.

For example:


The <input> Tag

This is the most commonly used tag within HTML forms. It allows you to specify various types of user input fields such as text, radio buttons, checkboxes, etc.

 

Text

text fields are used when you want the user to type short amounts of text into the form.

For example:


This repl.it shows the rendered results of the above code:

 

Radio Buttons

radio buttons are used when you want the users to select only one option from a pre-determined set of options.

Note that the users can select only one option. If they click on another option, the new option will be selected and the first option will be deselected.

If you want your users to be able to make multiple selections, use checkboxes instead (more on that later on in this checkpoint).

Here is the code for adding radio buttons to your form:


This repl.it shows the rendered results of the above code:

 

Checkboxes

Checkboxes are similar to radio buttons, but they enable your users to make multiple selections.

This is the code for adding checkboxes to your form:


This repl.it shows the rendered results of the above code:

 

Submit

The submit button allows the user to actually submit the form.

This is the code for adding a submit function to your form:


This repl.it shows the rendered results of the above code:

 

Select Lists

A select list is a dropdown list with options. This allows users to select one option from a list of pre-defined options.

The select list is created using the <select> tag in conjunction with the <option> tag.

This is the code for adding a select list to your form:


This repl.it shows the rendered results of the above code:

 

Textarea

You can use the <textarea> element to enable users to enter larger blocks of text than with the <input> tag.

It’s often a good idea to use the maxlength attribute to restrict the user’s input to a certain number of characters. You can also use the cols and rows attributes to adjust the width and height of a text input field.

This is the code for adding a textarea to your form:


This repl.it shows the rendered results of the above code:

 

Form Action

Usually when a user submits the form, you need the system to do something with the data. This is where the action page comes in. The action page is the page that the form is submitted to. This page could contain advanced scripts or programming that inserts the form data into a database or emails an administrator.

Creating an action page is outside the scope of this tutorial. In any case, many web hosts provide scripts that can be used for action page functionality. This includes emailing the webmaster whenever the form has been completed. For now, we will simply look at how to submit the form to the action page.

You nominate an action page with the action attribute.

This is the code for the action attribute in a form:


This repl.it shows the rendered results of the above code:

 

Form Method

You may have noticed that the above example uses a method attribute, method="get". This attribute specifies the HTTP method to use when the form is submitted.

Since the form data is appended to the URL upon submission, you can actually see the form variables in your browser’s address bar when the form is submitted. This can be handy for non-sensitive data such as a search engine’s results page. It also allows you to bookmark the page and even link to it from another webpage.

Another common form method is method="post". In this case, the form data is not appended to the URL upon submission. Providing the method="get" attribute is optional. If you don’t provide it, the method will default to method="post".

Mockup

Go over this article and learn how to implement a mockup using HTML andΒ CSS.