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

Mirroring input data back to the user

From: Validating and Processing Forms with JavaScript and PHP

Video: Mirroring input data back to the user

Our form has a bit of a problem. Although the validation feedback is awesome, we need to mirror the input from the form, back into the form. Otherwise when users make an error, the form will not send them back any of the fields they've already filled out. So let's see that at work. If we come over here to the form, and we just fill something out, I'll put in my name and I'm going to hit the Send button. In where is it tells me that the error that I forgot to put this in the right format. But I don't see my name anymore. so I would have to type that in, it's not such a big deal right now because obviously I have to retype that information.

Mirroring input data back to the user

Our form has a bit of a problem. Although the validation feedback is awesome, we need to mirror the input from the form, back into the form. Otherwise when users make an error, the form will not send them back any of the fields they've already filled out. So let's see that at work. If we come over here to the form, and we just fill something out, I'll put in my name and I'm going to hit the Send button. In where is it tells me that the error that I forgot to put this in the right format. But I don't see my name anymore. so I would have to type that in, it's not such a big deal right now because obviously I have to retype that information.

so I'll type it in the right format and then I'll put in a password that's just three letters. I'm going to hit Send and now it tells me that the name was fine, but it deleted it because it didn't remember it. And the password has to be six characters, and it deleted that as well. So, that's a problem. Let's go ahead and fix it. So first, I'm going to go into my index file, and I need to create variables for each of the form fields. And then pass the value of the post super global to those variables. These still say request, so first I'm going to change these to post. These fields are for my name, my password, and my password confirmation. To get the rest of them, I'm going to go into my code snippets file, and just copy them from right here.

I'll copy, then I'll paste them right there. And these names come from the input fields. There's one more thing we need to do. PHP doesn't like it, when we use a variable, that it doesn't know about. So when this page first loads, there's not going to be any post variables. So we shouldn't set these new variables, like my comments, to things that might not exist. So we're use is-set function to detect if a variable exists, and then assign it to a local variable. So I'm going to do one of these. I'll do an if statement here and then use the is set function.

And I'm going to check for the post superglobal with the value of my name. If it does exist, then I'll just create a new variable, just like I did before. And assign that value to that variable. So I need to do the rest of them, once again I'll just go into my code snippet. This is a lot of unnecessary typing, and just copy the rest of them. So now we're ready to take whatever the user types in when they fill out the form originally and add it to the value of our inputs.

That way when the form reloads it's going to remember everything. We'll need to go down to the input fields. I'll start with my name, and it's at the very end right before the self closing tag, I'm going to add the following. I'll add a value attribute which will be originally empty, and then I'm going to write some PHP right here. I'll check to make sure the my name variable exists. So, I'll do an isSet function and I'll check the variable myName, just to make sure that it's an existing variable. If that variable does exist, then I'll just echo it's value. So, let's go ahead and save this.

I'll reload my form and then I'll type in a name right here. Hit the sent button and you'll notice that now it's telling me there's an error in the format, but it remembered everything I typed in. So that if I typed it correctly, I'm going to hit Send, notice that the error goes away, but it remembers the value of that field. Now I am going into code snippets, and copy the rest of them. In our form, people have the option of choosing their favorite music. Now they can chose more than one of these items by clicking on these check boxes.

So if they're not already these check boxes should be returning into an array. Whenever we have a field that can have multiple values. They get fed into an array and we need to treat them a little bit differently than when we did right here when we want to re output those values. So, what I'm going to do is, I'll go over here to the end of this, and I'm going to hit return just to put that on a new line. And then I'm going to start typing some PHP right here. Once again, I'll make sure that the value of the variable has been set.

and now I need to check if the existing value of the check box, which in this case would be rock. Exists in the array that is returned into this variable, right here. I'm going to open up another set of parenthesis, and then I'll do an and comparison, and check to see using the in array function. If the value of this field, which is Rock, exists in our array. Now, if it does, what we want to do is echo the word Checked. When you echo the word Checked as part of an input field that is a check box, that item will automatically be checked when the form loads. So let me save this and I'll go ahead and reload this form. And this time, I'll click the rock checkbox to indicate that that's my favorite music.

If I hit send, it should remember that, even though it got some errors here. Now if I click on some of these other ones, it won't remember them. Notice that they disappear. So I need to go into my code snippets and copy that same code for the other values of the input fields. So to do this one, I'm going to go all the way to the end, right before the self-closing tag there, and just paste that code right there. I want this check that it outputs to be inside the input field. And then the last one. Select fields are like check boxes.

But we don't need to check an array unless your select fields have the multiple parameter turned on. These select fields don't. So we'll just make sure the variable reference has the right value. So right here, I'm going to add some PHP. I'll check to see if the reference variable has a value, and also that it matches the current input field. So this field happen to be friend, so we'll check that reference as a value of friend. If it does because this is a select option field, we'll add in the Selected keyword.

So we'll go ahead and echo the keyword Selected, and this option will become the default option once the page reloads. so let me save this and I'll reload the form. I'll chose a friend and hit the Send button, and you can see that it remembers that option just fine. So I'll go ahead and copy the rest of them from the code snippets. Next we'll do the Radio buttons. These Radio buttons are just like the select field. Unlike the checkboxes, they're not arrays, so they'll come back with a single value. So I'll go to the end here and add some PHP.

So this time, if the request type variable is set and also it happens to be the value of the current input field, in this case this is this is a question. Then I'll echo the keyword checked. Let me save that. I'll refresh my form and click on the question field here. So, it does remember that. We'll go ahead and copy the rest of them from the code snippets. The text area is just like a regular input field, but remember that the value of a text area goes in between the opening and closing text area tags. So, we're going to put our PHP right here.

Once again, I'll make sure that the my comments variable is set and if it is, I'll just output the variable. So I'm going to save this. Looks like I have an extra parenthesis here, so let me delete that. I'll save it, refresh the form. I'll type in a comment, and hit the Send button. And you can see that it's remembering the comment. I really hate it when I spend time filling out a form. And the form gives me an error, but doesn't remember some of the input. Mirroring input is the sort of thing that a lot of people will notice, unless you forget to do it.

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.