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

Working with form fields

From: Validating and Processing Forms with JavaScript and PHP

Video: Working with form fields

In order to understand how validation works, we must understand the different types of fields, and how the names and values associated with them get passed over to a form processor. By far, the most common type of field in HTML forms is the input field. Most of the communications and forms happen with input fields, because they can take on a variety of shapes. An input field by default, shows up as a text field, where you can type in values to be submitted to the form processor. It's not necessary, but you can add a type equals text attribute to the field. However, even without it, it will display by default, as a text field. Any form field should have a name associated with it. When you submit the field, that name will be sent along to the form processor with whatever is typed into the input field.

Working with form fields

In order to understand how validation works, we must understand the different types of fields, and how the names and values associated with them get passed over to a form processor. By far, the most common type of field in HTML forms is the input field. Most of the communications and forms happen with input fields, because they can take on a variety of shapes. An input field by default, shows up as a text field, where you can type in values to be submitted to the form processor. It's not necessary, but you can add a type equals text attribute to the field. However, even without it, it will display by default, as a text field. Any form field should have a name associated with it. When you submit the field, that name will be sent along to the form processor with whatever is typed into the input field.

Input fields can also have a value parameter. If you include this parameter, the value will be sent along with the field by default. In text typed fields, this means that the text will be prepopulated in the field. The value parameter is more useful for non text fields. You can add a label to any form field you want. The label is a way of associating some text, to an input field. One of the advantages of doing this, is that clicking on the label will also activate the form field. This could be really good for usability, since people on mobile devices will find it easier to tap on the label to activate the field. There's two ways of creating labels.

You can wrap the input field with a label tag, or you can use a for attribute on the label to link it to the input's field ID. So, in this case, this label right here is associated with this input field because of the ID and the for attribute. The text field is just one of the many varieties of input fields. So, let's take a look at some other ones. You've probably used an input field of type password; converts what you type into bullets. That way, others can't see what you're typing. Now, just like with text fields, any other field, if you want to, can have a label, a name, and a value. They work in the same way as the type equals text input field. You can also have input type equals file fields.

These are the traditional way of asking the user to upload a file. You will need to make sure that your form tag has the endtype attribute set to multipart form data. You'll also need to create a way for the file to be saved onto your server or for it to be included into a database. You can also create check boxes or radio buttons. Check boxes let you ask questions where the user can check one or more options. So this is great for asking questions that can have one or more answers. The form processor will receive a variable with each name value pair and each input field. If you include a checked attribute, one of the input fields can be prechecked. This works with check-boxes and input type radio buttons. Radio buttons are very much like check-boxes but they are meant for questions where the answer should be exclusive.

So this is great for asking a question like rate this from 1-10. Notice that if I click on one of these input fields and then click on the other one it deselects the original one. You achieve this by making sure each input field has the same name but different values. There is a type of field that is invisible and it's the input type equals hidden field. Its a great way to send data to your servers that the users can't see. Say for example that somebody orders a product from your page and you want to pass along a part number. You don't need the user to know what that is.

Simply create this field with a name and a value, and when the form is submitted the name and value will be passed to the processor just like with any other input field. There is a number of fields that let you create applicable buttons. The most famous is the input type submit field. When the button is clicked it sends a form to the processor. You can add a value field and that becomes the label of the field. So instead of submit, it now says click me. Just like with any other input field, you can also add a name and the name and value will get passed to the processor. You've probably also seen the input type equals reset field. When you click it, it will clear out what the user has typed in the current form. Now, there's another way to create buttons and this is not an input field. It's a special tag called the button tag.

It looks very much like the input type submit and input type reset but, it's a set of open and closed HTML tags. The label of that button is whatever is in between those tags. Now, there's even a button field that can look like a photo and it's called the imple type image field. When you click on the image, the form will be submitted. You should include both a source attribute to specify the location of the image, as well as an out attribute field. So, you can't do everything with just input fields. the text area field allows you type longer pieces of text. The text that you type inside this field will be sent along with the name of the field through the form processor.

This field, like the button field, has a beginning and an ending part of the tag. If you place something in between these two tags, it prefills the text area field. In addition to the text area field, there's a set of tasks for creating a drop down menu of options. Use the select tag and place an option tag inside it. Notice that the name goes in the select tag. But the values go inside each option field. What's going to be sent to the form processor is not what you place in the labels. But instead, whatever you include in the option tags value attribute.

So when you submit this form, if you select the item number 2, the form processor will receive a variable called My Options with the value, B. If you include a selected attribute in an option field, that field will automatically be selected when the form loads. There's another version of the select field. It has an added attribute of multiple. That allows you to select more than one item at a time. You hold down the Cmd key on the Mac or the Ctrl key on the PC to do this. And this way, it's sort of like the check box input field. When the data gets sent to the processor, it sends the name, along with a comma separated list of selected options.

This one's a little trickier to process than others. HTML5 adds a number of special input fields. This is a growing list but some of the fields are pretty cool. For example you can use an input type equals date to get a popup calender. Input type color let's you choose a color from a color picker. Input type range lets you choose with a slider. The thing to remember about HTML5 fields is that browser support is not uniform. So although these fields look great on this browser which is Chrome, it might not look as well on Internet Explorer or some other browsers.

HTML5 also has a number of attributes to input elements like autocomplete, placeholder, required, pattern and others. Support for these attributes are spotty on older browsers. We'll talk more about these in the chapter on HTML5 validation. You may also want to check out the course HTML5: Web Forms in Depth with Joe Marini. HTML5 has a number of fields you can use to create form elements. It's important to get used to how the different fields work, and what happens when the forms are submitted.

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.