You are here: Home » All Posts » HTML Forms » HTML Form Tutorial

HTML Form Tutorial

in HTML Forms

While other elements of HTML gives style and meaning to your website, an HTML form adds interactivity. HTML forms handle important functions like taking orders, surveys, user registration and more. You will hardly find a single web site without forms.

How does an HTML form work?

A web form has two parts: the HTML ‘front end’ and a back end form processor. The HTML front end part handles the presentation while the back end handles the form submissions (like saving the form submissions, sending emails etc).
The back end form processor script is usually written in languages like PHP, ASP or Perl.

The image below illustrates the concept:
Illustration : web form working

  1. A visitor visits a web page that contains a form.
  2. The web browser displays the HTML form.
  3. The visitor fills in the form and submits
  4. The browser sends the submitted form data to the web server
  5. A form processor script running on the web server processes the form data
  6. A response page is sent back to the browser.

The HTML form tag

All the input elements should be enclosed within the opening and closing <form> tags like this:

<form>

The input elements go here….

</form>

The following are the attributes of the form tag:

action=”Link to the form processor script”
The action attribute points to the server side script (the ‘back end’) that handles the form submission. Usually, this will be a script (PHP,ASP, Perl) or a CGI program.

For more information, see: Switching HTML form action field dynamically

method =get|post ( either GET or POST)
In simple terms, if you use GET method, the form submission values are passed as part of the URL. If it is POST, the information is sent to the server as part of the data body and will not be visible in the URL box in the user’s browser.
If you don’t specify the method, GET is taken by default.

Suppose your form handler page is a Perl script named formmail.pl. the HTML form code would be:

<form action="cgi-bin/formmail.pl" method="post">
................................
.....your input items here .....
................................
</form>

Read more about the HTML Form tag here and about the GET and Post methods here.

The form input elements

You can have different types of input elements in a form. Examples are: check boxes, radio buttons, simple text boxes etc.

Let us see how to create input elements for a form.

Single line text box

text input

A single line text box can be used to collect the name, email, phone number etc from your web site visitors.

Here is the code to create a simple text box:

<input type="text" name="FirstName" />

type=”text”
the ‘type’ attribute tells the browser that a single line text input box should be created.

name=”FirstName”
gives a name to the field. The name is used to identify the field on the server side.

There are some more attributes that you can use with the text box

value=”default value”
The text you give as value will be displayed by default in the text box.
Example:

<input TYPE="text" name="FirstName" value="Your FirstName here,Please" />

maxlength=”maxChars”
Specifies the maximum number of characters the user can enter into this text box.

Let us expand our previous HTML form with some text boxes.

<form action="cgi-bin/formmail.pl" method="post">
   <p>
   Name: <input type="text" name="FirstName" value="" size="25" maxlength="50" />
   </p>
   <p>
   Email: <input type="text" name="Email" value="" size="25" maxlength="50" />
   </p>
</form>

There are two fields in this form for collecting the name and email address of the visitor. The <p> tags are to break the input elements in to two lines.

Submit button

HTML form submit button

After entering the data, the user presses the submit button which triggers the browser to send the data to the server. You can add a submit button to the form using the ‘submit’ input type.

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

name =”submit”
There can be more than one submit buttons in a form. On the server side, the submit button which was pressed can be identified using the ‘name’ attribute.

value=”Submit”
The string given in the ‘value’ attribute is displayed as the label of the Submit button.

Let us put it together to make a complete form page:
HTML form tutorial example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns='http://www.w3.org/1999/xhtml'>
   <head >
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
      <title >Form Page: sampleform</title>
   </head>
<body>
<h1>Sample form page</h1>

<form id='sampleform' method='post' action='' >
   <p>
   Name: <input type='text' name='Name' />
   </p>
   <p>
   Email: <input type='text' name='Email' />
   </p>
   <p>
   <input type='submit' name='Submit' value='Submit' />
   </p>
</form>

</body>
</html>

Copy this code to an HTML file and open it in your favorite browser.

See the form in action:
HTML form tutorial example

Next part: HTML Form Tutorial Part II : More Input Elements

Related posts:

  1. HTML Form Tutorial Part III : Still More Input Elements
  2. HTML Form Tutorial Part IV: Server Side Form Processing
  3. HTML Form Tutorial Part II : More Input Elements
  4. The HTML Form Tag
  5. The HTML Form Submit Button
  6. How to switch the 'action' field in an HTML form dynamically
  7. From an HTML Form to an Email Inbox
  8. How to get the value of a form element using JavaScript
  9. How to make a web form
  10. How to use getElementById to get the elements in a form
  11. How to Submit a Form Using JavaScript
  12. Using JavaScript to reset or clear a form
  13. Using JavaScript to access form objects when there are multiple forms
  14. Can JavaScript email a form?
  15. How to set the value of a form field using Javascript PII

{ 29 comments… read them below or add one }

AdeelAhmed January 6, 2012 at 12:29 pm

This is good website for learning .

Reply

JavaSurfer January 2, 2012 at 7:15 am

–Very Clear and easy to understand.
I am looking for java script that will
point to a reply page on submit of
the form that reads: Ex: Thank
You. Your information has been
received. A representative will
review your request and contact
you within 48 hours. etc…

Reply

vinod December 16, 2011 at 9:39 am

Hi, this is vinod.These examples are easy to understand…..nice

Reply

deepa December 16, 2011 at 8:50 am

This is one of the awesome site which i ever seen

Reply

chet December 15, 2011 at 9:52 am

very good representation of form

Reply

Wasantha December 15, 2011 at 4:15 am

This explains everything clearly and I used this to have a contact form to my site, thanks.

Reply

Visitor November 2, 2011 at 12:57 am

Thank you!
It was really helpful for a beginner like me :)

Reply

michael patterson October 27, 2011 at 3:51 pm

thanks for the help trying to do a school science fare now i can awnser a billion more questions……HERE COME 1 PRIZE AND AN A+++!

Reply

Hal Robson Kanu October 19, 2011 at 7:54 am

This is one of the awesome site which i ever seen……….i just understood all the concepts clearly…….and i am eager to read all the other chapters in it…….keep rocking….:-)

Reply

carmela October 4, 2011 at 1:32 pm

tenx tlga it really helped me a lot u know.,I was so stress coz this was our final project in our IT. muaah

Reply

Max October 1, 2011 at 5:01 pm

Thanks a lot for the tutorial…..it was very helpful?

Reply

john ubatay July 20, 2011 at 10:23 am

nc tutorial. thanks!

Reply

Ravikumar June 1, 2011 at 1:11 pm

I used this code, but tried to enter more than 3 inputs all txt boxes (visit, fname, lname, email, comment), and the form wouldn’t go through. It goes through with 3 inputs but not 4. Is there a limit on number of inputs fir formmail.pl ?

Reply

Ksue May 22, 2011 at 3:59 pm

Oh, and I’m using a validate function with this, up in my , that works with all 4 inputs. (i.e. empty field gives error message.. opportunity to fill the field before form is sent.)

Reply

Ksue May 22, 2011 at 3:56 pm

I used this code, but tried to enter more than 3 inputs all txt boxes (visit, fname, lname, email, comment), and the form wouldn’t go through. It goes through with 3 inputs but not 4. Is there a limit on number of inputs fir formmail.pl ?

Reply

Ashok kumar jhasya April 27, 2011 at 7:08 am

supurab man… even a lay man can also learn the thing from here

Reply

Winarno April 17, 2011 at 12:00 pm

after someone press the submit bottom, how the browser can open new page like tank for submiting and other

Reply

barbara April 4, 2011 at 2:58 pm

wow,thanks for the tutorial

Reply

Vipit March 30, 2011 at 1:48 pm

Thank you for this tutorial! It is great when someone explains these things in a way that even I can understand it :) Thanks!

Reply

lalli March 16, 2011 at 5:22 pm

This is one of the awesome site which i ever seen……….i just understood all the concepts clearly…….and i am eager to read all the other chapters in it…….keep rocking….:-)

Reply

Leave a Comment

Next post: