Professional Documents
Culture Documents
HTML Forms are required, when you want to collect some data from the site visitor.
For example, during user registration you would like to collect information such as
name, email address, credit card, etc.
A form will take input from the site visitor and then will post it to a back-end application
such as CGI, ASP Script or PHP script etc. The back-end application will perform
required processing on the passed data based on defined business logic inside the
application.
There are various form elements available like text fields, textarea fields, drop-down
menus, radio buttons, checkboxes, etc.
The HTML <form> tag is used to create an HTML form and it has following syntax −
<form action = "Script URL" method = "GET|POST">
form elements like input, textarea etc.
</form>
<form>
.
form elements
.
</form>
<input type="radio"> Displays a radio button (for selecting one of many choices
All the different input types are covered in this chapter: HTML Input Types.
Text Fields
The <input type="text"> defines a single-line input field for text input.
Example
A form with input fields for text:
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname">
</form>
Program:
<html>
<body>
<form>
</form>
<p>Also note that the default width of text input fields is 20 characters.</p>
</body>
</html>
Output:
Last name:
Doe
Also note that the default width of text input fields is 20 characters.
The <label> Element
Notice the use of the <label> element in the example above.
The <label> element also help users who have difficulty clicking on very small
regions (such as radio buttons or checkboxes) - because when the user clicks
the text within the <label> element, it toggles the radio button/checkbox.
Radio Buttons
The <input type="radio"> defines a radio button.
Example
A form with radio buttons:
<html>
<body>
<h2>Radio Buttons</h2>
<form>
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<label for="other">Other</label>
</form>
</body>
</html>
Radio Buttons
Male
Female
Other
Checkboxes
The <input type="checkbox"> defines a checkbox.
Example
A form with checkboxes:
<html>
<body>
<h2>Checkboxes</h2>
</form>
</body>
</html>
Checkboxes
The input type="checkbox" defines a checkbox:
I have a bike
I have a car
I have a boat
Submit
The form-handler is typically a file on the server with a script for processing
input data.
<html>
<body>
<h2>HTML Forms</h2>
<form action="/action_page.php">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a page called
"/action_page.php".</p>
</body>
</html>
HTML Forms
First name:
John
Last name:
Doe
Submit
If you click the "Submit" button, the form-data will be sent to a page called
"/action_page.php".
If the name attribute is omitted, the value of the input field will not be sent at
all.
Example
This example will not submit the value of the "First name" input field:
<html>
<body>
<form action="/action_page.php">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a page called
"/action_page.php".</p>
<p>Notice that the value of the "First name" field will not be submitted, because
the input element does not have a name attribute.</p>
</body>
</html>
The name Attribute
First name:
John
Submit
If you click the "Submit" button, the form-data will be sent to a page called
"/action_page.php".
Notice that the value of the "First name" field will not be submitted, because the input
element does not have a name attribute.