HTML Forms

An HTML form is used to collect user input. The user input is most often sent to a server for processing.

Why use HTML Form?

HTML forms are required if you want to collect some data from of the site visitor.

For example: If a user want to purchase some items on internet, he/she must fill the form such as shipping address and credit/debit card details so that item can be sent to the given address.

 

The HTML <form> element is used to create an HTML form for user input:
 
<form>
.
form elements
.
</form>
 
 
The <form>  element is a container for different types of input elements, such as: text fields, checkboxes, radio buttons, submit buttons, etc.
 
 

HTML Form Tags



HTML <input> element

The HTML <input> element is fundamental form element. It is used to create form fields, to take input from user. We can apply different input filed to gather different information form user. Following is the example to show the simple text input.

Example:

<!DOCTYPE>

<html>

<title>HTML Tutorials</title>

<body>

<form>  

     Enter your name  <br>  

    <input type="text" name="username">  

  </form>

</body>  

</html>

Output



HTML TextField Control

The type="text" attribute of input tag creates textfield control also known as single line textfield control. The name attribute is optional, but it is required for the server side component such as JSP, ASP, PHP etc.

Example

<!DOCTYPE>

<html>

<title>HTML Tutorials</title>

<body>

<form>  

   <label for="fname">First name:</label><br>

   <input type="text" name="firstname"/> <br> 

   <label for="lname">Last name:</label><br>

   <input type="text" name="lastname"/> <br>  

  </form>

</body>  

</html>

Output




The <label> Element

The <label> tag defines a label for many form elements.

The <label>  element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focus on the input element.


HTML <textarea> tag in form

The <textarea> tag in HTML is used to insert multiple-line text in a form. The size of <textarea> can be specify either using "rows" or "cols" attribute or by CSS.

Example

<!DOCTYPE>

<html>

<title>HTML Tutorials</title>

<body>

<form>  

   <label for="address">Enter Your Address:</label><br>

    <textarea rows="2" cols="20"></textarea>  

  </form>

</body>  

</html>

Output


HTML Password Field Control

The password is not visible to the user in password field control.

Example

<!DOCTYPE>

<html>

<title>HTML Tutorials</title>

<body>

<form>  

   <label for="password">Password: </label>  

   <input type="password"  name="password"/> <br/>  

  </form>

</body>  

</html>

Output


Radio Button Control

The radio button is used to select one option from multiple options. It is used for selection of gender, quiz questions etc.

If you use one name for all the radio buttons, only one radio button can be selected at a time.

Using radio buttons for multiple options, you can only choose a single option at a time. 

Example

<!DOCTYPE>

<html>

<title>HTML Tutorials</title>

<body>

<form>  

   <label for="gender">Gender: </label>  

              <input type="radio"  name="gender" value="male"/>Male  

              <input type="radio" name="gender" value="female"/>Female <br/>  

  </form>

</body>  

</html>

Output


Checkbox Control

The checkbox control is used to check multiple options from given checkboxes.

Example

<!DOCTYPE>
<html>
<title>HTML Tutorials</title>
<body>
<form>  
   Hobby:<br>  
              <input type="checkbox" id="cricket" name="cricket" value="cricket"/>  
                 <label for="cricket">Cricket</label> <br>  
              <input type="checkbox" id="football" name="football" value="football"/>  
                 <label for="football">Football</label> <br>  
              <input type="checkbox" id="hockey" name="hockey" value="hockey"/>  
                 <label for="hockey">Hockey</label>  
  </form>
</body>  
</html>

Output



Submit button control

HTML <input type="submit"> are used to add a submit button on web page. When user clicks on submit button, then form get submit to the server.

Syntax: 

<input type="submit" value="submit">  

Example

<!DOCTYPE>

<html>

<title>HTML Tutorials</title>

<body>

<form>  

   <label for="name">Enter name</label><br>  

    <input type="text" id="name" name="name"><br>  

    <label for="pass">Enter Password</label><br>  

    <input type="Password" id="pass" name="pass"><br>  

    <input type="submit" value="submit">

  </form>

</body>  

</html>

Output


HTML <fieldset> element:

The <fieldset> element in HTML is used to group the related information of a form. This element is used with <legend> element which provide caption for the grouped elements.

Example: 

<!DOCTYPE>

<html>

<title>HTML Tutorials</title>

<body>

<form>  

   <fieldset>  

      <legend>User Information:</legend>  

    <label for="name">Enter name</label><br>  

    <input type="text" id="name" name="name"><br>  

    <label for="pass">Enter Password</label><br>  

    <input type="Password" id="pass" name="pass"><br>  

    <input type="submit" value="submit">  

</fieldset>  

  </form>

</body>  

</html>

Output




Following is the example for a simple form of registration.

<!DOCTYPE>
<html>
<title>HTML Tutorials</title>
<body>
 <h2>Registration form</h2>  
    <form>  
     <fieldset>  
        <legend>User personal information</legend>  
        <label>Enter your full name</label><br>  
        <input type="text" name="name"><br>  
         <label>Enter your email</label><br>  
         <input type="email" name="email"><br>  
         <label>Enter your password</label><br>  
         <input type="password" name="pass"><br>  
         <label>confirm your password</label><br>  
         <input type="password" name="pass"><br>  
         <br><label>Enter your gender</label><br>  
         <input type="radio" id="gender" name="gender" value="male"/>Male  <br>  
         <input type="radio" id="gender" name="gender" value="female"/>Female <br/>    
         <input type="radio" id="gender" name="gender" value="others"/>Others <br/>   
          <br>Enter your Address:<br>  
         <textarea></textarea><br>  
         <input type="submit" value="Sign-up">  
     </fieldset>  
  </form>  
</body>  
</html>

Output





















 

No comments:

Post a Comment