How to create a form in HTML

problem

You wish to collect user information from a web page, e.g. registering a user, login in. This can be done with an Html form and a user filling that form.

SOLUTION

Using the <form> tag. This tag has an opening and a closing tag and it acts like a container. For example, we put inside all the input fields, labels, etc we wish to have on our form. This way, the browser will know what to send to the backend.

Let’s see an example of user login.

Typically, we ask the user for:

  • Email
  • Password
  • Remember me
form.html
				
					<form action="/" method="POST">
    <label for="txtemail">Email:</label>
    <input type="email" name="txtemail"/>
    <br/>
    <label for="txtpassword">Password:</label>
    <input type="password" name="txtpassword"/>
    <br/>
    <label for="chkremember">Remember me</label>
    <input type="checkbox" name="chkremember"/>
    <input type=”submit”>submit</input>
</form>

				
			
output
Html form for login example

In order to submit a form, we have to place a submit button inside the form. This is an input type=”submit” and appears as a button as shown with the message “Login test” above.

When the form is submitted, the web browser sends the data of the form to the URL specified by the path attribute on the form. 

The method attribute identifies which HTTP method to use, e.g. POST.

Post the data to a backend - the postman test API
form.html
				
					<form action="https://postman-echo.com/post" method="POST">
				
			

This is provided by Postman.

running it
form post example on postman echo api
output
json response from html post

We can see the JSON response from the test API. Basically, it echoes back the form data that we sent over along with extra information like headers, etc.

				
					"form": {
    "txtemail": "testemail@a.com",
    "txtpassword": "sdddd",
    "chkremember": "on"
  }

				
			
Making the form more user friendly with autofocus

We can specify the focus on the first textbox when the web page loads, guiding the user where to type first without having to click inside the textbox to gain focus. 

Just by adding the autofocus attribute.

For example:

form.html
				
					            <input type="email" name="txtemail" autofocus />

				
			
output
form and autofocus on input types html example
Adding tabindex on our input controls

We can use the attribute tabindex to specify the order each HTML (input) control has when the user is pressing the tab. Simply by using incremental numbers for tabindex as shown below.

Full code:

form.html
				
					<html DOCTYPE!>
    <body>
        <form action="https://postman-echo.com/post" method="POST">
            <label for="txtemail">Email:</label>
            <input type="email" name="txtemail" tabindex="1" autofocus />
            <br/>
            <label for="txtpassword">Password:</label>
            <input type="password" name="txtpassword" tabindex="2" />
            <br/>
            <label for="chkremember">Remember me</label>
            <input type="checkbox" name="chkremember" tabindex="3" />
            <br/>
            <input type="submit" value="Login test" tabindex="4" />
        </form>
    </body>
</html>

				
			
output
using tabindex in the form

conclusion

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.