HTML Forms: When you need to collect some data from the user then HTML forms are required. For example, during user registration, you need to collect information of the user like name, address, Phone number, etc. A form takes input from the user and then post it to the back-end application(CGI, ASP script or PHP script).
HTML Forms
Processing is done on the passes data at the back-end. There are various forms available like text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc.
Syntax:
<form action = “Script URL” method = “GET|POST”>
form elements like input, textarea, etc.
</form>
HTML FORMS ATTRIBUTES
Following is the list of commonly used form attributes:
Tags | Description |
---|---|
action | Process your passed data. |
method | GET and POST methods are used to upload data. |
target | Specifies the target window where the result of the script will be displayed. |
enctype | To specify how the browser encodes the data before it sends it to the server. |
HTML FORM CONTROLS
There are different types of form controls to collect data. They are:
- Text Input Controls
- Checkboxes Controls
- Radio Box Controls
- Select Box Controls
- File Select boxes
- Hidden Controls
- Clickable Buttons
- Submit and Reset Button
TEXT INPUT CONTROLS
There are three types of text input controls. They are:
1. Single-line text input controls
It is used for items that require only one line of user input, such as search boxes or names. They are created using HTML <input> tag.
2. Password input controls
This is also a single-line text input but it masks the character as soon as a user enters it. They are also created using HTML <input> tag.
3. Multi-line text input controls
It is used when the user is required to give details that may be longer than a single sentence. Multi-line input controls are created using HTML <textarea> tag.
Single-line text input control Attributes
Single-line text input controls are required at only one line of user input(such as search boxes or names). They can be created using HTML <input> tag.
Following is the list of attributes for <input> tag for creating a text field.
Attributes | Description |
---|---|
type | indicates the type of input control |
name | gives a name to the control |
value | provides an initial value inside the control |
size | specifies the width of the text-input control in terms of characters |
maxlength | specifies a maximum number of characters a user can enter into the text box. |
Example:
<!DOCTYPE html> <html> <head> <title>Text Input Control</title> </head> <body> <form > Address: <input type = "text" name = "address" /> </form> </body> </html>
HTML Password input control Attributes
This is also a single-line text input but it masks the character as soon as a user enters it. They are also created using HTML <input>tag but type attribute is set to password.
Following are the list of attributes for <input> tag for creating a text field.
Attributes | Description |
---|---|
type | indicates the type of input control |
name | gives a name to the control |
value | provides an initial value inside the control |
size | specifies the width of the text-input control in terms of characters |
maxlength | specifies a maximum number of characters a user can enter into the text box. |
Example:
<!DOCTYPE html> <html> <head> <title>Password Input Control</title> </head> <body> <form > user name : <input type = "text" name = "user_name" /> <br> Password: <input type = "password" name = "password" /> </form> </body> </html>
Multiple-Line Text Input Controls Attributes
Multiple-Line text input controls are used when the user needs to give details that may be longer. Multi-line input controls are created using HTML <textarea> tag.
Following are the list of attributes for <textarea> tag:
Attributes | Description |
---|---|
name | gives a name to the control |
rows | indicates the number of rows |
size | specifies the width of the text-input control in terms of characters |
Example:
<!DOCTYPE html> <html> <body> <form> <textarea rows = "7" cols = "30" name = "description"> Enter the information </textarea> </form> </body> </html>
Check-box Control Attributes
Check-boxes are used when a user wants to select more than one option. They are created using HTML <input> tag and the type attribute is set to a checkbox.
Following are the attributes for <checkbox> tag
Attributes | Description |
---|---|
type | indicates the type of input control |
name | gives a name to the control |
value | the value that is used if the checkbox is selected |
checked | if you want to select it by default then it is set to checked |
Example:
<!DOCTYPE html> <html> <body> <form> <input type = "checkbox" name = "HTML" value = "on"> HTML <input type = "checkbox" name = "CSS" value = "on"> CSS </form> </body> </html>
Radio Button Control Attributes
Radio buttons are used when a user wants to select only one option out of many options. They are also created using HTML <input> tag and the attribute type is set to the radio.
Following are the list of attributes for radio buttons
Attributes | Description |
---|---|
type | indicates the type of input control |
name | gives a name to the control |
value | the value that is used if the checkbox is selected |
checked | if you want to select it by default then it is set to checked |
Example:
<!DOCTYPE html> <html> <head> <title>Radio Box Control</title> </head> <body> <form> <input type = "radio" name = "Gender" value = "Male"> Male <input type = "radio" name = "Gender" value = "Female"> Female </form> </body> </html>
Select Box Control Attributes
Select box control is referred to as a drop-down box. This provides an option to list down various options in the form of a drop-down list, where user can select two or more options.
Following are the list of attributes for select box control
Attributes | Description |
---|---|
size | to present a scrolling list box. |
name | gives a name to the control |
multiple | allows the user to select multiple items from the menu. |
Example:
<!DOCTYPE html> <html> <head> <title>Select Box Control</title> </head> <body> <form> <select name = "dropdown"> <option value = "HTML" selected>HTML</option> <option value = "CSS">CSS</option> </select> </form> </body> </html>
File Upload Box Attributes
File upload box is also known as file select box. You need to create a file upload box if you want to allow the users to upload a file to your website. This is created using <input> element but type attribute is set to file.
Following are the list of attributes for file upload box
Attributes | Description |
---|---|
name | used to give name to the control that is sent to the server to get recognizes and get the value |
accept | mentions the type of file that the server accepts. |
Example:
<!DOCTYPE html> <html> <head> <title>File Upload Box</title> </head> <body> <form> <input type = "file" name = "HTML" accept = "image1" /> </form> </body> </html>
Button Controls Attributes
You can create a clickable button using <input>tag by setting its type attribute to button.
Following are the list of attributes for button controls
Attributes | Description |
---|---|
submit | automatically submits a form |
reset | automatically resets form controls to their initial values |
button | used to trigger a client-side script when the user clicks that button. |
image | creates a clickable button but we can use an image as the background of the button. |
Example:
<!DOCTYPE html> <html> <head> <title>File Upload Box</title> </head> <body> <form> <input type = "submit" name = "submit" value = "Submit" /> <input type = "reset" name = "reset" value = "Reset" /> <input type = "button" name = "ok" value = "OK" /> <input type = "image" name = "imagebutton" src = "/html/images/image.png" /> </form> </body> </html>
Hidden Forms Controls
Hidden form controls hide data inside the page which later on can be pushed to the server. This control does not appear on the actual page, it hides inside the code. For example, When a user clicks next page then the value of hidden control is sent to the web server and there it decides which page will be displayed next based on the passed current page.
Example:
<!DOCTYPE html> <html> <head> <title>File Upload Box</title> </head> <body> <form> <p>This is page 10</p> <input type = "hidden" name = "pagename" value = "15" /> <input type = "submit" name = "submit" value = "Submit" /> <input type = "reset" name = "reset" value = "Reset" /> </form> </body> </html>