Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92174 Viewers
82 Video lessons · 104053 Viewers
71 Video lessons · 75366 Viewers
56 Video lessons · 106742 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.
Your file was successfully uploaded.