Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Introducing forms

From: HTML Essential Training

Video: Introducing forms

The forms interface is extremely important in HTML. It's how HTML does interactivity. There are quite a few new features in HTML5, but the fundamental idea has not changed since the first web browsers. You still have text fields, buttons, and controls. Let's make a working copy of forms.html, and we are going to call this forms-working.html. I am going to open this in the text editor. There are basically two ways to use forms in HTML. Traditionally, they were mostly used to interface with a process on a server using CGI.

Introducing forms

The forms interface is extremely important in HTML. It's how HTML does interactivity. There are quite a few new features in HTML5, but the fundamental idea has not changed since the first web browsers. You still have text fields, buttons, and controls. Let's make a working copy of forms.html, and we are going to call this forms-working.html. I am going to open this in the text editor. There are basically two ways to use forms in HTML. Traditionally, they were mostly used to interface with a process on a server using CGI.

Today, they are just as often used to interface with JavaScript running in the client. Most of our examples in this chapter will be interacting with JavaScript locally, in the browser. This is really for convenience so that you may follow along with the exercises without requiring a server. Some of the exercises later in the chapter will use a server, and I provided examples of a simple CGI script in PHP, Python, and Perl for you to see how that works. You notice up at the top of this file we use this forms.css. That's some styling for these forms.

We will take a look at that in a moment. We also have forms.js, which is the JavaScript that we will be using throughout most of this chapter in order to look at the results of our forms. And then we have this symbol form. It has a text field, a password field, some checkboxes, some radio buttons and a submit button. Then we have a div down here at the bottom for where the results go. So let's take a look at this in the browser. We will look at this example in Firefox, but will be using different browsers throughout this chapter.

So this is what it looks like in Firefox, and when I type something in here--and I will type in too in the password, press the big red button; you can tell it's a big red button because it's labeled Big Red Button--and you see there are all of our results. Now an important note here: if you are using a browser that doesn't support the forms validation API, you will get a little notice at the top here that your browser doesn't support the forms validation API. And most of these exercises will work just fine; just the validation features won't work, and we do use those and look at those throughout the chapter.

The reason for that is the forms validation API is really incredibly useful. It will save you a lot of JavaScript work in your applications, and it soon will be supported on the latest versions of all the browsers. Right now, the only one that it's not really working on, the latest version of as of this recording, is Internet Explorer. It is slated to be implemented in Internet Explorer 10 though, so if you're watching this course after Internet Explorer 10 comes out, then it should work just fine. Just to give me an example, fortunately, all the browsers here on this Mac support the interface, but I've got an old version of Firefox.

So I am just going to close this version of Firefox, and I am going to open up this old version of Firefox, so that you can see what this looks like with a browser that doesn't support this API. So here it is, in an old version of Firefox, and if I bring up the About, you will see it's Version 1.5.07. And if I just type some stuff in here--one, two--you see everything works as we expect, because these are forms elements that have been around since the very beginning of forms. When I press the Big Red Button, you see I get this red notice, "This platform does not support the HTML5 validation API," and we still get all of our values.

We just won't get red messages when the validation doesn't work. So if you have a browser that doesn't support the HTML5 forms validation API, this is what you'll see, and that should be just fine. And that includes versions of Internet Explorer up to the version 9. So I'm going to close this and just open it up in Google Chrome so you can see what that looks like. And here is Google Chrome and if I type in one, two, and press the Big Red Button, you see that all works exactly as we expect.

So here in our HTML, you see at the top here, we have a div outer and a div form. The outer is actually not visible on the screen here. It's just there for positioning so that we can center these two elements, and it contains them also so that when I shrink it down, they don't go one on top of the other. They stay side to side. Then we have the form, and that's the one on the left. And then we have the results, and that's the one on the right. And if we look at the CSS, so our forms.css here, this is mostly just my reset CSS that I have used throughout this course.

It's got a few little changes in it, and most of that is down here at the bottom. You see outer has a width of 800 pixels and a margin that centers it and positions it five pixels from the top. And then the form and the results, they both float left, and that allows them to do this nice little stacking trick where one floats to the left first and then the next one floats to left right next to it. And it sets its background color. And then we have just a few other stylings the results we wanted in this monospace fonts so it comes up looking rational for a programmer, and so we can see the spacing of things.

And that's really all there is to that CSS. Let's take a look at the JavaScript now. This is forms.js, and this is the JavaScript that we are using throughout this course for most of you exercises. And like so much of my JavaScript, it really starts at the bottom, and that's just the way that it works well for me. So window.onload = init, that just means that it waits for everything to load up in the browser before it calls init, and then it calls init. And of course it needs these elements to exist before it's going to do its initialization; that's why it has to wait for the onload event.

So it sets up our results and form elements, and it checks to see if we have the validation API. Checking to see if you have the validation API is a simple matter of checking to see if you have this validity object and its type is actually an object, and I'm checking that on just the first of the form elements on my eForm. Remember, eForm is this object here which is found by this f1 element id, and that is in our HTML right here.

So that's this form element. So it checks for the first element and checks if it has this validity object and if it does, then we have the forms validation API. Now, whenever we press this button or press Enter in the form, it returns the results of this display function, and the display function is this one here. It clears the output, which will clear this here. You remember, when you first loaded it, it said Results go here. So we want to clear that, or if we press the button several times with different values, we want it to clear and show us our new values.

And it checks if you don't have validation, it puts up that error message. And it resets a global OK flag so that it will go ahead and validate again, and then it just steps through the form elements. If you don't have validation, it just displays the output, and if you do, it actually checks the validity, and it's as simple as that. There is a display value function, which does different things for radio buttons and checkboxes and things like that, just so that it gets the intelligible values out of these things. You can look through that if you want to see how that's done, and here's how we check the validity.

We actually just check this valid property from the validity object, and if it's true then it's valid and if it's not true, then we display an error message. It's really as simple as that. It's a very simple API. There's a few other things too it, but that's the simple way to use it, at least for our purposes. And then these are just our output routines for displaying an output. As an error, it uses this class error, which turns it red in our CSS and otherwise, it just displays a paragraph with the output string. So this is a very simple JavaScript, and it works great for our purposes, so that when we fill in our forms we get intelligent results.

Now while I've got this form up, I just want to show you a few new attributes we have in the forms API. I am going to switch back to our forms here, and you'll notice this autofocus. When I load up the form for the first time, it puts the cursor right there in that text box, so I don't have to run any JavaScript rigmarole anymore. I just put that attribute in and it will autofocus right on that element. There is also a required attribute. So if I say required here in my text form and I save this and reload it in the browser, now this text element is required.

If I leave it blank and press the Big Red Button, you see we get an error. It says, "Please fill out this text field," and that's all from the validation API. There's also a lovely new placeholder attribute, and it looks like this. If I save that and reload, you see we have this little placeholder now that says your name. And the minute you start typing, the placeholder goes away and the value gets replaced. And if you just leave it blank, of course the your name is not used.

It's not the same as putting in a value. Again, this is something we had to do all kinds of rigmarole before in order to put in a default value of some sort or a placeholder and have JavaScript that would detect the minute you type something in and take that out and replace it, and it was a whole big rigmarole. We don't have to do that anymore. We have this lovely placeholder attribute. And finally, there is a readonly attribute. So if I actually put a value in here, because this doesn't make sense without a value-- say value = foo bar baz, and readonly-- now when I reload this, you see we have that value in there, but you can't type anything in it, and yet the value shows up.

So this is useful for places where you might want to have a value that only under some circumstances it becomes editable, and your JavaScript can switch that flag on and off. Most of the forms interface in HTML will work with either JavaScript or CGI. In JavaScript, you access the values of forms elements using the standard document object model interface. You can learn more about JavaScript here on the lynda.com online trading library in Simon Allardice's excellent JavaScript Essential Training.

CGI stands for the Common Gateway Interface. This is the protocol that forms use to communicate with a script on a web server. You can learn more about CGI here on lynda.com online training library in my own CGI Essential Training.

Show transcript

This video is part of

Image for HTML Essential Training
HTML Essential Training

82 video lessons · 98529 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 5m 24s
    1. Welcome
      56s
    2. Using the exercise files
      1m 37s
    3. What you need to know about this course
      2m 51s
  2. 22m 0s
    1. What is HTML?
      4m 12s
    2. Examining the structure of an HTML document
      7m 50s
    3. Understanding tags and containers
      6m 4s
    4. Exploring content models in HTML5
      2m 23s
    5. Looking at obsolete elements
      1m 31s
  3. 27m 19s
    1. Understanding whitespace and comments
      3m 53s
    2. Displaying text with paragraphs
      3m 37s
    3. Applying style
      8m 5s
    4. Using block and inline tags
      6m 34s
    5. Displaying characters with references
      5m 10s
  4. 16m 36s
    1. Exploring the front matter of HTML
      2m 9s
    2. Applying CSS to your document
      3m 59s
    3. Adding scripting elements
      4m 54s
    4. Using the meta tag
      3m 34s
    5. Optimizing your page for search engines
      2m 0s
  5. 24m 59s
    1. Controlling line breaks and spaces
      2m 46s
    2. Exploring phrase elements
      1m 44s
    3. Using font markup elements
      1m 5s
    4. Highlighting text with mark
      1m 29s
    5. Adding headings
      1m 38s
    6. Using quotations and quote marks
      3m 2s
    7. Exploring preformatted text
      1m 45s
    8. Formatting lists
      2m 28s
    9. Forcing text direction
      3m 49s
    10. Suggesting word-break opportunities
      2m 29s
    11. Annotating East Asian languages
      2m 44s
  6. 29m 15s
    1. Introducing CSS
      55s
    2. Understanding CSS placement
      6m 55s
    3. Exploring CSS syntax
      10m 34s
    4. Understanding CSS units of measure
      3m 3s
    5. Some CSS examples
      7m 48s
  7. 22m 5s
    1. Using images
      4m 13s
    2. Flowing text around an image
      4m 55s
    3. Breaking lines around an image
      3m 3s
    4. Aligning images
      5m 25s
    5. Mapping links in an image
      4m 29s
  8. 22m 28s
    1. Understanding URLs
      2m 41s
    2. Working with hyperlinks
      3m 28s
    3. Using relative URLs
      4m 20s
    4. Specifying a base URL
      2m 19s
    5. Linking within a page
      4m 12s
    6. Using image links
      5m 28s
  9. 17m 2s
    1. Exploring list types
      3m 52s
    2. List elements in depth
      7m 44s
    3. Using text menus with unordered lists
      5m 26s
  10. 15m 30s
    1. Introduction to HTML semantics
      4m 9s
    2. Exploring an example
      4m 56s
    3. Marking up figures and illustrations
      2m 33s
    4. Creating collapsible details
      3m 52s
  11. 11m 18s
    1. Embedding audio
      5m 19s
    2. Embedding video
      5m 59s
  12. 11m 53s
    1. Creating ad-hoc Document Object Model (DOM) data with the data-* attribute
      4m 53s
    2. Displaying relative values with meter
      2m 57s
    3. Creating dynamic progress indicators
      4m 3s
  13. 4m 49s
    1. Overview of HTML5 microdata
      1m 8s
    2. Exploring an example with microdata
      3m 41s
  14. 7m 3s
    1. Understanding outlines
      52s
    2. A demonstration of outlining
      6m 11s
  15. 13m 1s
    1. Table basics
      7m 29s
    2. Exploring the semantic parts of a table
      2m 32s
    3. Grouping columns
      3m 0s
  16. 9m 55s
    1. Frames overview
      54s
    2. Using traditional frames
      4m 26s
    3. Exploring inline frames using iframe
      2m 7s
    4. Simulating frames with CSS
      2m 28s
  17. 53m 7s
    1. Introducing forms
      10m 24s
    2. Using text elements
      10m 12s
    3. Using checkboxes and radio buttons
      2m 37s
    4. Creating selection lists and dropdown lists
      5m 14s
    5. Submit and button elements
      8m 48s
    6. Using an image as a submit button
      2m 15s
    7. Keeping context with the hidden element
      3m 0s
    8. Setting tab order
      2m 7s
    9. Preloading an autocomplete list using the datalist feature
      5m 26s
    10. Displaying results with output
      3m 4s
  18. 19m 47s
    1. Touring a complete site
      2m 14s
    2. Touring the HTML
      8m 44s
    3. Touring the CSS
      8m 49s
  19. 29s
    1. Goodbye
      29s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.