The <form> Element
The HTML <form> element is used to create an HTML form for user input:
<form>
<input type=”text” name=”txtFname”/>
</form>
The <form> element is a container for different types of input elements, such as: text fields, checkboxes, radio buttons, dropdown, submit buttons, etc.
Form Attributes:
The Action Attribute
The action attribute decides the action to be performed when the form is submitted.
Normally, the form data is sent to the server when the user clicks on the submit button.
Example
<form action=”/action_page.php”>
The Target Attribute
The target
attribute specifies where to display the response on the same tab or different tag that is received after submitting the form.
The target
attribute can have one of the following values:
The default value is _
self
which means that the response will open in the current browser window or Tab.
Example
Here, the submitted result will open in a new browser tab:
<form action=”/action_page.php” target=”_blank”>
Value | Description |
_blank | The response is displayed in a new window or tab |
_self | The response is displayed in the current window |
_parent | The response is displayed in the parent frame |
_top | The response is displayed in the full body of the window |
framename | The response is displayed in a named iframe |
The Method Attribute
The method
attribute specifies the HTTP method to be used when submitting the form data.
The form-data can be sent as URL variables with method="get"
or as HTTP post uses method="post"
The default HTTP method when submitting form data is GET.
Example
This example uses the GET method when submitting the form data:
<form action=”/action_page.php” method=”get”>
<form action=”/action_page.php” method=”post”>
Notes on GET:
- It Appends the form data to the URL, in name/value pairs
http://localhost:8080/action_page.php?fname=test
- Do not use GET method to send sensitive data! (the submitted form data is visible in the URL!)
- The length of a URL is limited (2048 characters)
- GET is good for non-secure data
Notes on POST:
- Appends the form data inside the body of the HTTP request (the submitted form data is not shown in the URL)
- POST has no size limitations and can be used to send large amounts of data.
- Form submissions with POST cannot be bookmarked.
The Novalidate Attribute
The novalidate
attribute is a boolean attribute.
When we define novalidate, it specifies that the form-data (input) should not be validated when we submitted data.
<form action=”/action_page.php” novalidate>
The HTML <form> Elements
The HTML <form> element can contain one or more of the following form elements:
Tag | Description |
<form> | Defines an HTML form for user input |
<input> | Defines an input control |
<textarea> | Defines a multiline input control (text area) |
<label> | Defines a label for an <input> element |
<fieldset> | Groups related elements in a form |
<legend> | Defines a caption for a <fieldset> element |
<select> | Defines a drop-down list |
<optgroup> | Defines a group of related options in a drop-down list |
<option> | Defines an option in a drop-down list |
<button> | Defines a clickable button |
<datalist> | Specifies a list of pre-defined options for input controls |
<output> | Defines the result of a calculation |
HTML Input Types
Here are the different input types you can use in HTML:
- <input type=”text”>
- <input type=”button”>
- <input type=”submit”>
- <input type=”email”>
- <input type=”image”>
- <input type=”checkbox”>
- <input type=”color”>
- <input type=”date”>
- <input type=”datetime-local”>
- <input type=”file”>
- <input type=”hidden”>
- <input type=”month”>
- <input type=”number”>
- <input type=”password”>
- <input type=”radio”>
- <input type=”range”>
- <input type=”reset”>
- <input type=”search”>
- <input type=”tel”>
- <input type=”time”>
- <input type=”url”>
- <input type=”week”>
HTML Input Attributes
The value Attribute
The input value
attribute specifies an initial value for an input field:
Attribute | Description |
checked | It specifies that an input field should be pre-selected when the page loads (for type=”checkbox” or type=”radio”) |
disabled | It specifies that an input field should be disabled |
max | It specifies the maximum value for an input field |
maxlength | It specifies the maximum number of characters for an input field |
min | It specifies the minimum value for an input field |
pattern | It specifies a regular expression to check the input value against |
readonly | It specifies that an input field is read only (cannot be changed) |
required | It specifies that an input field is required (must be filled out) |
size | It specifies the width (in characters) of an input field |
step | It specifies the legal number intervals for an input field |
value | It specifies the default value for an input field |
Sample Example:
1)
Using Get method and input type text, password, submit and value and name attribute
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="home.html" method="get">
UserName:
<input type="text" name="txtUsername" value="Type Username" /> <br>
Password:
<input type="password" name="txtPassword" /><br>
<input type="submit">
</form>
</body>
</html>
2)Using Post method and input type text, password, submit and value and name attribute
Recommended method for login form because of sensitive data username and password, so value never append in URL as a query parameter and it hides from user.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="home.html" method="post">
UserName:
<input type="text" name="txtUsername" value="Type Username" /> <br>
Password:
<input type="password" name="txtPassword" /><br>
<input type="submit">
</form>
</body>
</html>
Output:
For Get method example:
After click on the submit you can notice browser URL something like below URL and your password clearly visible this is drawback of using get method for sensitive information.
http://127.0.0.1:5500/home.html?txtUsername=abc6663&txtPassword=password123
3) Simple Registration form:
This is simple registration form and most of the common input type used. You can experiment with adding additional attribute and type in this form. Use form post method in registration form because you don’t even know what is size of data you are going to submit.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="home.html" method="post">
Registration form:
<table cellpadding="5" cellspacing="5" border="1">
<tr>
<td>Name</td>
<td> <input type="text" name="txtName" /></td>
</tr>
<tr>
<td>Email</td>
<td> <input type="email" name="txtemail" /></td>
</tr>
<tr>
<td>Phone</td>
<td> <input type="text" name="txtPhone" /></td>
</tr>
<tr>
<td>Count</td>
<td> <input type="number" name="txtNumber" /></td>
</tr>
<tr>
<td>Gender</td>
<td>
Male<input type="radio" name="radioGender" value="Male"/>
Female<input type="radio" name="radioGender" value="Female"/>
</td>
</tr>
<tr>
<td>Hobbiles</td>
<td>
<input type="checkbox" name="chkMusic" />Music
<input type="checkbox" name="chkSports" />Sports
<input type="checkbox" name="chkReading" />Reading
</td>
</tr>
<tr>
<td>Occupation</td>
<td>
<select name="selOccupation">
<option value="">--Select--</option>
<option value="engineer">Engineer</option>
<option value="accountant">Accountant</option>
</select>
</td>
</tr>
<tr>
<td>Upload File</td>
<td>
<input type="file" name="fileupload" />
</td>
</tr>
<tr>
<td> <input type="submit"> <!-- or you can use button <input type="button" onclick="callFunction();"--> </td>
<td>
<input type="reset" Value="Clear Data">
</td>
</tr>
</table>
</form>
</body>
</html>
Output:
Feel free to read and comment below.
Below is my created application and is useful for people having Google Opinion Rewards.
https://play.google.com/store/apps/details?id=com.manasvi.sawant.rewardtocash
If you wanted to create a website, please visit my fiverr gig link below.