HTML Forms are required when you want to collect some data from the site visitor. For example registration information: name, email address, credit card, etc.
A form will take input from the site visitor and then will post your back-end application such as CGI, ASP Script or PHP script etc. Then your back-end application will do required processing on that data in whatever way you like.
Form elements are like text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc. which are used to take information from the user.
A simple syntax of using <form> is as follows:
<form action="back-end script" method="posting method"> form elements like input, textarea etc. </form>
Form Attributes:
- name:Â This is the name of the form.
- action:Â Here you will specify any script URL which will receive uploaded data.
- method:Â Here you will specify the method to be used to upload data. It can take various values but most frequently used are GET and POST.
- target:Â It specifies the target page where the result of the script will be displayed. It takes values like _blank, _self, _parent etc.
- enctype:Â You can use the enctype attribute to specify how the browser encodes the data before it sends it to the server. Possible values are like:
- application/x-www-form-urlencoded – This is the standard method most forms use. It converts spaces to the plus sign and non-alphanumeric characters into the hexadecimal code for that character in ASCII text.
- mutlipart/form-data – This allows the data to be sent in parts, with each consecutive part corresponding the a form control, in the order they appear in the form. Each part can have an optional content-type header of its own indicating the type of data for that form control.
Form controls:
- Text input controls
- Buttons
- Checkboxes and radio buttons
- Select boxes
- File select boxes
- Hidden controls
- Submit and reset button
1. HTML Forms – Text Input Controls:
There are actually three types of text input used on forms:
- Single-line text input controls:Â Used for items that require only one line of user input, such as search boxes or names. They are created using the <input> element.
- Password input controls:Â Single-line text input that mask the characters a user enters.
- Multi-line text input controls:Â Used when the user is required to give details that may be longer than a single sentence. Multi-line input controls are created with the <textarea> element.
2. HTML Forms – Button:
There are various ways in HTML to create clickable buttons. You can create the clickable button using <input> tag.
When you use the <input> element to create a button, the type of button you create is specified using the type attribute. The type attribute can take the following values:
- submit:Â This creates a button that automatically submits a form.
- reset:Â This creates a button that automatically resets form controls to their initial values.
- button:Â This creates a button that is used to trigger a client-side script when the user clicks that button.
Example:
<form action="#" method="get"> First name: <input type="text" name="first_name" /> <br> Last name: <input type="text" name="last_name" /> <input type="submit" value="submit" /> <input type="reset" value="Reset" /> </form>
3. Checkbox:
Checkboxes are used when more than one option is required to be selected. They are created using <input> tag as shown below.3. Checkboxes Control:
Example:
<form action="#" method="get"> <input type="checkbox" name="maths" value="on"> Maths <input type="checkbox" name="physics" value="on"> Physics <input type="submit" value="Select Subject" /> </form>
4. Radio Buttons
Radio Buttons are used when only one option is required to be selected. They are created using <input> tag as shown below:4. Radio box Control:
<form action="" method="post"> <input type="radio" name="subject" value="maths" /> Maths <input type="radio" name="subject" value="physics" /> Physics <input type="submit" value="Select Subject" /> </form>
5. Select box Control:
Drop Down Box is used when we have many options available to be selected but only one or two will be selected.
Example:
<form action="" method="post"> <select name="dropdown"> <option value="Maths" selected>Maths</option> <option value="Physics">Physics</option> </select> <input type="submit" value="Submit" /> </form>
6. File Select Boxes:
If you want to allow a user to upload a file to your web site from his computer, you will need to use a file upload box, also known as a file select box. This is also created using the <input> element.
<form action="" method="post" name="fileupload" enctype="multipart/form-data"> <input type="file" name="fileupload" accept="image/*" /> </form>
7. Hidden Controls:
If you will want to pass information between pages without the user seeing it. Hidden form controls remain part of any form, but the user cannot see them in the Web browser.
<form action="/deepak/hello_get.php" method="get" name="pages"> <p>This is page 10</p> <input type="hidden" name="pgaenumber" value="10" /> <input type="submit" value="Next Page" /> </form>
Sample Registration Form in HTML
<html> <head> <title>This is title for form</title> </head> <body> <form action="#" name="StudentRegistration"> <table cellpadding="2" width="20%" bgcolor="99FFFF" align="center" cellspacing="2"> <tr> <td colspan=2> <center><font size=4><b>Student Registration Form</b></font></center> </td> </tr> <tr> <td>Name</td> <td><input type=text name=textnames id="textname" size="30"></td> </tr> <tr> <td>Father Name</td> <td><input type="text" name="fathername" id="fathername" size="30"></td> </tr> <tr> <td>Postal Address</td> <td><input type="text" name="paddress" id="paddress" size="30"></td> </tr> <tr> <td>Personal Address</td> <td><input type="text" name="personaladdress" id="personaladdress" size="30"></td> </tr> <tr> <td>Sex</td> <td><input type="radio" name="sex" value="male" size="10">Male <input type="radio" name="sex" value="Female" size="10">Female</td> </tr> <tr> <td>City</td> <td><select name="City"> <option value="-1" selected>select..</option> <option value="New Delhi">NEW DELHI</option> <option value="Mumbai">MUMBAI</option> <option value="Goa">GOA</option> <option value="Patna">PATNA</option> </select></td> </tr> <tr> <td>Course</td> <td><select name="Course"> <option value="-1" selected>select..</option> <option value="B.Tech">B.TECH</option> <option value="MCA">MCA</option> <option value="MBA">MBA</option> <option value="BCA">BCA</option> </select></td> </tr> <tr> <td>District</td> <td><select name="District"> <option value="-1" selected>select..</option> <option value="Nalanda">NALANDA</option> <option value="UP">UP</option> <option value="Goa">GOA</option> <option value="Patna">PATNA</option> </select></td> </tr> <tr> <td>State</td> <td><select Name="State"> <option value="-1" selected>select..</option> <option value="New Delhi">NEW DELHI</option> <option value="Mumbai">MUMBAI</option> <option value="Goa">GOA</option> <option value="Bihar">BIHAR</option> </select></td> </tr> <tr> <td>PinCode</td> <td><input type="text" name="pincode" id="pincode" size="30"></td> </tr> <tr> <td>EmailId</td> <td><input type="text" name="emailid" id="emailid" size="30"></td> </tr> <tr> <td>DOB</td> <td><input type="text" name="dob" id="dob" size="30"></td> </tr> <tr> <td>MobileNo</td> <td><input type="text" name="mobileno" id="mobileno" size="30"></td> </tr> <tr> <td><input type="reset"></td> <td colspan="2"><input type="submit" value="Submit Form" /></td> </tr> </table> </form> </body> </html>