HTML Essential Training (2017)


The <form> Element



HTML forms are used to collect information from the user.
Forms are defined using the <form> element, with its opening and closing tags:
Syntax:-
<body>
  <form>…</form>
</body>

The method and name Attributes



The method attribute specifies the HTTP method (GET or POST) to be used when forms are submitted (see below for description):

<form action="url" method="GET">
<form action="url" method="POST">


To take in user input, you need the corresponding form elements, such as text fields. The <input> element has many variations, depending on the type attribute. It can be a text, password, radio, URL, submit, etc.

The example below shows a form requesting a username and password
CODE:-
<form>
   <input type="text" name="username" /><br />
   <input type="password" name="password" />
</form>


Form Elements



If we change the input type to radio, it allows the user select only one of a number of choices:

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


The type "checkbox" allows the user to select more than one option:

<input type="checkbox" name="gender" value="1" /> Male <br />
<input type="checkbox" name="gender" value="2" /> Female <br />


The submit button submits a form to its action attribute:

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

Example:-
<html>
<body>

<h2>HTML Forms</h2>

<form action="/action_page.php">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="John"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Doe"><br><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>

OUTPUT:-

HTML Forms