We launched a new IT training category! Check out the 140+ courses now.

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

Accessing forms

From: Validating and Processing Forms with JavaScript and PHP

Video: Accessing forms

Accessing forms is similar to accessing other HTML elements but because of their nature there are some additional methods and properties we can take advantage of. If you'e given your form an ID, the easiest way to have access to the form element is by using getElementById, but with forms, you can also look up elements by name. So here's a form I've created with a number of different types of fields. If you're interested in how this form was put together, make sure you check out my course on CSS styling forms. I put together this form on chapter six. So I'm using the Google Chrome browser and I need to look at the Developer tools.

Accessing forms

Accessing forms is similar to accessing other HTML elements but because of their nature there are some additional methods and properties we can take advantage of. If you'e given your form an ID, the easiest way to have access to the form element is by using getElementById, but with forms, you can also look up elements by name. So here's a form I've created with a number of different types of fields. If you're interested in how this form was put together, make sure you check out my course on CSS styling forms. I put together this form on chapter six. So I'm using the Google Chrome browser and I need to look at the Developer tools.

I'm going to right click on this form and select Inspect Element from the pop up menu. From here, I need to go to the JavaScript Console which is the last tab. Now let's add some code to send the form information to the JavaScript Console. This form has an ID of my forms, that's what I'll type in here. So we were successfully able to target the form itself, and all the elements are accessible underneath that element. Of course if an element has an ID you can get just that element with the same method. Our first field has a name of my name. So let's see if we can target that.

Since I'm using Google Chrome, I can hit the up arrow to get to the last command. So, I'll just modify this to my name, and hit return, and now I get just the input field with the user's name. This is one of the reasons why it's a good idea to add IDs to your form elements. Of course, you can also use the getElementByTagName method to target all the input fields in our form. So, let's try that. And I'm going to add for all the input tag names. Now we get a list of all the input fields. And if you'll notice a little bracket's on each end of our list, you can tell that this is an array form. So, if I hit the up arrow, and I attach some brackets, and then type the index of the element that I want, I'll ask for the first element. Then I can get just to that field which is the same as the field we had before. This is the name that we're asking the user for. Now this will list all the forms in the current page. This page has only one form so there's only one element. This is probably a dangerous way to access a form since the number of forms on a page can vary from time to time.

It's probably better to refer to it by its ID. This form has an ID of My Form, so I can just type this, and I get the form again. So although the preferred way to access elements with JavasScript is usually by working with IDs, forms also have a special attribute called Name. Using the document forms array, you can access an element by name instead of by index number. We can even shorten this further by adding a name to the form itself. This form has a name of The Form.

Now to get to the name field we can simply type, document.theform.myname. If we try to access an element with multiple values like a radio button you can get an array of elements. Here's a couple of radio buttons. The name of those buttons are, request type. So here we get an array of two input fields, and if we want to access one of them, we can use an array notation. Here's the first one, and here of course is the second one. If we try to access an element like a select field we'll get just the select field itself.

So we have select field called reference. Now we don't get the individual options inside reference but we can target those with an array like notation. So to get the first option, I will type in array0. And that gives me the choose option which is the default. You can see some of the other options here. So I can get to those by typing 1, 2 or 3. To get to each of the options use an array notation similar to what we did with radio buttons. Most validation deals with accessing specific form elements usually by using an ID, however sometimes if you use names in your forms you can get to them quicker by using the document forms array.

Show transcript

This video is part of

Image for Validating and Processing Forms with JavaScript and PHP
 
Expand all | Collapse all
  1. 3m 36s
    1. Welcome
      1m 9s
    2. What you should know
      1m 2s
    3. Using the exercise files
      1m 25s
  2. 12m 50s
    1. Understanding forms
      2m 2s
    2. Working with form fields
      7m 4s
    3. Using the form tag
      3m 44s
  3. 19m 23s
    1. Using input validation
      2m 10s
    2. Adding required fields and placeholders
      1m 31s
    3. Constraining numeric and date fields
      2m 32s
    4. Accepting multiple entries
      1m 41s
    5. Limiting uploads by MIME types
      2m 35s
    6. Assisting text input with a datalist
      1m 55s
    7. Constraining with regular expression patterns
      6m 59s
  4. 42m 13s
    1. Accessing forms
      3m 57s
    2. Looking up form elements
      3m 35s
    3. Handling focus changes
      2m 47s
    4. Detecting the onchange event
      4m 31s
    5. Using the selectedIndex property
      2m 30s
    6. Dynamic validation with regular expressions
      7m 0s
    7. Creating a generic input validation function
      4m 31s
    8. Validating in older browsers with Modernizr
      7m 32s
    9. Interrupting form submission with onsubmit
      5m 50s
  5. 15m 20s
    1. Understanding jQuery
      3m 47s
    2. Validating on submit with jQuery
      3m 45s
    3. Building interactive jQuery validation
      2m 34s
    4. Using the jQuery Validation plugin
      5m 14s
  6. 32m 57s
    1. Communicating with PHP servers
      2m 27s
    2. Retrieving data from superglobals
      8m 18s
    3. Using server-side validation
      4m 59s
    4. Adding in-page validation
      5m 22s
    5. Mirroring input data back to the user
      7m 46s
    6. Sanitizing form input
      4m 5s
  7. 43m 29s
    1. Mailing form data
      8m 28s
    2. Understanding file uploads
      3m 1s
    3. Uploading files
      9m 20s
    4. Processing form data with AJAX
      8m 14s
    5. Preparing your database
      5m 50s
    6. Pushing data
      8m 36s
  8. 1m 17s
    1. Next steps
      1m 17s

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.

Join now Already a member? Log in

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 Validating and Processing Forms with JavaScript and PHP.

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 preferences from 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.