What does form do in HTML?


A HTML form is used to get user input on a website(or web app) and then use the input to interact with a server. The HTML form element does three things:
    1. Holds and contain the elements that will collect the input.
    2. point to where the collected data (from the form) will be sent.
    3. Indicates how the data is sent to the server(Method).
An example of a HTML form will look like this.
<form action="/action_page.php" method="get">
    First name: <input type="text" name="fname"><br>
    Last name: <input type="text" name="lname"><br>
    <input type="submit" value="Submit">
Let’s look at these three ideas in details

1. Indicate where the data goes(ACTION)

The form has an attribute called action. The action attribute contains a URL. This URL is where the data is being sent by the form. The action attribute must be a valid url. The URL can be a relative path(“/contacts/clients.php”) or an absolute path (“https://avicndugu.com”).
If you don’t include the action attribute the URL becomes the URL of the page containing the form.

2. Holds form elements

HTML forms can contain different kind of html elements. Some of these elements are:
<input>                 <textarea>
<button>                <select>
<option>                <optgroup>
<fieldset>              <label>

3. how the data is sent to the server(Method)

On the web, you can use two methods to send data to the server. These are:

GET Method:

You can use GET method to ask the server to send back some information.
Any data you submit in this form will appear as part of the URL.
An example of a get request is the google search. If you look at the URL when you do a google search, you will see what you typed appear there.

POST Method:

You can use the post method when you need to submit sensitive information like passwords, credit card number etc.
Your data does not appear in the URL and is therefore considered secure.

What does form action mean in HTML?

The form ‘action’ is a form attribute that is a URL that indicates where the data in the form will be sent to.

What is form action and method in HTML?

Form action specifies where your form will send the data while method specifies the way in which you send data.

Where do you use form tags?

HTML forms can be used to:
  1. Fill multiple choice questions: Commonly used in surveys, personality test etc
  2. Fill essay questions: Commonly used when applying for school, scholarship program etc.
  3. Collect data.
  4. Login/ Sign up: Used on any site that requires you to become a member.
author's bio photo

Hi there! I am Avic Ndugu.

I have published 100+ blog posts on HTML, CSS, Javascript, React and other related topics. When I am not writing, I enjoy reading, hiking and listening to podcasts.