Placeholder Image

Subtitles section Play video

  • information is key, which is why forms are one of the most important parts of a website.

  • In today's video.

  • I'm gonna show you everything you need to know about how to create forms and elements inside of those forms.

  • And if you're new around here, make sure you subscribe for more videos where I simplified the Web for you.

  • Let's get started now to get started with forms.

  • Let's build one of the most common types of forms, which is a log in form.

  • To do that, we first need to create the page that load it on.

  • So we just read a page called index dot html and in here, if you're envious code or you're using Emmett, you just type exclamation point and hit enter.

  • It'll generate all the boiler quite code you need for a basic HTML page.

  • We could just change the title here to form because this is going to be demonstrating forms now to use a form.

  • The first thing you need to do is open up a form tag so you could just type in here form.

  • Close it off, and this is going to be a form tagging all the inputs that you put inside of this form are going to be submitted to wherever your form goes to by default.

  • If you just leave your foreign tape blank like this, it's going to submit to the page that you're currently on.

  • So in her study here, let's put our different inputs so we can use the input element, which is going to allow us to input into our page from our user.

  • And what we want to do is we want an input element, and this one's going to be the name.

  • And the thing with input elements is they don't actually need to be closed because they don't actually allow you to specify anything inside of the tags, their self closing tags.

  • So if we want to add a label tore input we need to do is we need to use what's called a label element.

  • So let's have our 1st 1 be our user name.

  • So what, you're gonna put name here, and we could just copy this down because we're going to need a 2nd 1 which is going to be for a password so we can type in a password here and now we're almost complete with the basics of reform.

  • But we need some way to submit our forms, so we're going to create a button to do that.

  • So let's just create a button here and we're gonna call this submit for the text of our button.

  • Now, to actually test this page, we can just right click and open with Live Server, which is an extension from visual studio code that you can download once we have that open.

  • We can see over here that if we expand this a little bit, we have our name field.

  • We have our password field and we have our submit button, and right now they're all clobber it onto the same line.

  • So let's use some dibs, which is just another basic tech which will allow us to separate these onto multiple lines.

  • So if we put some code inside of this, Div is going to end up on the next line and we could do the exact same thing down here.

  • They're password.

  • Let's create another def to wrap our password element in whoops.

  • Copy this paste in there.

  • And when we say you see it populates over here on new lines.

  • Now we can enter name information passed on information and click submit and you'll see it'll submit it to the page that were already owned.

  • But while technically this does work, it's not at all how you want to build forms.

  • In actuality, almost always when you build a form, you're going to want your foreign to us a bit somewhere other than the page you're currently on.

  • So I wanted to do that.

  • We have a key word here in attributes, which is called Action, and the action is going to be where you're for Miss submitting to and in our case we're just going to create a page here, which is called results dot html, and this is going to be what we want our form to submit to.

  • The next thing that you almost always air.

  • Gonna specify on a form is the method you want your form to use, and this is either going to be get or post would get request.

  • It's going to upend things to the girl, and it's going to send it to another page in your sight.

  • While the Post is going to be useful for when you have a server where you need to save some information if, for example, we would use post here.

  • So if we change this to post and we try to submit something you see that were immediately going to get an era because browsers can't render post request, they can only render get request and we don't have a server.

  • So what we need to do is we need to use get request here instead of a post request, and now this is going to work as soon as we create our actual page.

  • So let's create that results dot html page and I'm actually going to copy over here.

  • A result html page.

  • This is a very basic page.

  • All it has is a link that goes back to our form and has a little bit of Java script, which is going to render the results from our HTML form from before.

  • Now let's go back over here, refresh our page, and we want to just make sure we click submit and you'll see that nothing is being shown up for our form fields.

  • We do have that button to go back, and the reason nothing is showing up for our form fields is because in order for an input to actually show up as a form.

  • What we need to do is we need to specify the name for that input.

  • So let's do that now.

  • The name here is there's going to be a name for this 1st 1 because we just wanted to be whatever we're submitting to our form.

  • So we're gonna use this as name and down here for a password.

  • We're gonna set the name of this one equal to password.

  • Now, if we say bet and come over here and we type in, for example, a user name and we type in a password, let's just say that's that and we click submit, you'll see Name is set to user name and password is set to password.

  • And if you can see in the euro, it's setting those parameters.

  • Here.

  • Name equals user name and password equals password.

  • And I wouldn't get request.

  • Does is it sets it in the Earl here while if we did the host request here, this is actually going to set it in the body.

  • So if we refresh this and turn some information and click, submit again, we're going to get that error because we don't have a server accepting any requests.

  • So you need a server if you're gonna use Post for the restless example we're going to be using get for all of our different requests.

  • So now, with that out of the way, we have our form set up properly.

  • We ever inputs with actual names so we can use these and render them later when we submit our form.

  • But our labels aren't actually doing anything very useful because normally if you click on the label, it should actually highlight the field that it's being labeled for.

  • This is really helpful for not only users but also for screen readers.

  • In order to do this, we just need a set of four attribute here, and we said It's the idea of the element that we want Thio highlight whenever we click on the label.

  • So in our case, we're gonna say name and then we need to give our element here that I D.

  • Which is just going to be named now if we click on our label, it's actually gonna highlight the input element that that label was for another way.

  • That you can do this is if you don't wanna have that for a tribute.

  • You can actually nest your element inside the label.

  • So if we come in here and we wrap our label around our input element, so we have both are Password String and our input owner inside of the label.

  • Now, when we quit the label, you see it's going to highlight that password field for us.

  • Almost always, you're going to see people use the four attributes instead of wrapping inside of it just because it's cleaner and easier to style.

  • But just so you know that you can do this and a lot of people do this when it comes to check boxes and radio buttons, which we're going to get to later in this tutorial.

  • And speaking of different type of input elements, one thing young you knows is that our input here does not have any type specified for it, which is why it's defaulting to a text input here.

  • But you always want to be explicit in what type of input you're using.

  • So you always want to specify a type field, and the input tag is very versatile because you could have many, many different types of inputs.

  • But in our example, we just wanna have a text and put here for the name.

  • But for the password.

  • Normally, when you're on a website, when you type in your password, it's actually hidden.

  • And that's because there's a type which is called Password, which, actually by default, hides the characters for you.

  • So now if we save this and we enter our user name, you see it works just fine when we enter a password.

  • You see it's hiding our characters for us, and that's because we're using type of password for input element.

  • And lastly, the only thing left to make this log inform correct is that we need to change our button down here because we want this to be a submit button so we can just come in here.

  • We can type in the type of submit, and what this says is this says it's going to submit our form, and when we hit, enter, for example, anywhere in our form.

  • So if I type in something here and I clicked the enter key, it's also going to submit our form, which is why having types of mint on your button is really useful and it also allows you to have other buttons in your form that don't necessarily submit your form, because this one right here is submitting it.

  • Since it has the type of submit, another type of button that's really useful and really under no one is going to be a button with a type of reset.

  • And if we just set that here, we're gonna set the text here to reset and we go back to our form and we just type in some texts.

  • If we clicked, reset, it sets everything back to its defaults.

  • And as you can see, our defaults for us, art is going to be completely blank.

  • But what happens if, for example, you want to default the field for the user?

  • Let's say you already know what the user name is for that user, and you want to set that by default.

  • That's where the value property is going to come in.

  • So you just type in value and anything you said in here is going to be the default value for that property.

  • So what set the default here to just be user Now, when we're your fresh, our page, you see the user is being the default.

  • That name, and the best part is, is if we change us and click reset, it goes back to the default value we have set in our code for the HTML.

  • Another thing that's really useful is have a placeholder value.

  • For example, if we have this user's name here, let's say that we don't actually know what their name is, but we want to put in place older.

  • This is just something that's going to show up to kind of let the user know a little bit more information about what they want to put in here.

  • So we're gonna say our place holder is just user name.

  • And as you can see, it feels that in the latest great text, and as soon as the user types something else, a completely removes the place for their.

  • And if they remove everything, it goes back to having that placeholder.

  • Just to reiterate a little bit on the points that we touched on a placeholder is really useful for when you want to give a little bit more information to your user while value that you set.

  • So, for example, we change this here to value.

  • This is really useful.

  • If you already know what the value is or you want a default.

  • This is something that is going to be submitted with your form because of a click.

  • Submit.

  • It submits that user name value.

  • But if we change is to be a placeholder, for example, instead and we click submit, it does not submit the place holder only the value.

  • Another thing you'll notice is that we're able to submit a blank name in a blank password, which in a log in form, you would want to throw some kind of error to the user, telling them they can't submit blank information.

  • And luckily, this is really easy to do.

  • There's an attributes which is called required and is required.

  • Attributes is forcing the user to actually enter this information.

  • Let's remove this place holder so it's a little easier to see.

  • And if we save it and click submit, you'll see that we get an error that says police fall this field, and if we fill it out and click, submit now to let it go through.

  • So we want to add that required attributes to both our password and to our name field here.

  • So now they Even if they enter just a name, no password.

  • It's still gonna give in that air until they enter that password as well.

  • Now, as I mentioned earlier in this video, the input element here has many, many different types that it can be.

  • So let's go through an analyze some of the different types that we have by starting with one of the most common types, which is going to be the email type.

  • Let's create another def.

  • Here.

  • This dip is going to be used to ask for the email.

  • So we're gonna have a label.

  • We're gonna use four email and in here we're just gonna put email as the label, and then we're gonna create in our input, and this is going to have a type here of email, and this is going to verify email addresses for us by default.

  • Let's make sure we set the name to email so we can use it when we submit it.

  • And we also need the i d here to match what we set for our four.

  • So we're gonna use email in there, and let's just make it required because we always want our user to send an email.

  • Now if we say that you see our email field is showing up here, and if we type in something, for example, was typing everything and you click submit, you see that it says to please actually make sure that this is a valid email address.

  • So by default, our browser is doing email validation for us now.

  • It's not the greatest female validation in the entire world, but it's a whole lot better than having nothing.

  • And also, when a user's on a phone, it's going to give them a keyboard, which is actually specified for entering emails as opposed to just the generic text keyboard.

  • Other than that, though, email is very similar to the text field that we've already covered.

  • Now let's move on to a little bit more complicated of Field, which is the number field.

  • So it's again enter another Devyn here with the label.

  • This is going to be for what we're going to put a czar age because we want to ask the user for their age and we're going to create input again for doing that, and this is going to have a type here of number and what's make sure reset a name, which is going to say age and an I D, which is going to be a JJ.

  • And let's just not make this from required because we don't think that they need to enter this.

  • And if we say you say we get a JJ and immediately you'll notice we have up and down arrows to feel to specify where age is going as well as we can use the arrow keys to modify this.

  • And that's because we're using the type of number when we specify this input.

  • Also, if I try to enter different characters that are not numbers, it won't allow me to enter them.

  • Another really useful thing that we can do when specifying number elements, as we can specify Minimum and maximum Sze.

  • So obviously we don't want her age to be below zero, So we're gonna set the minimum here to be one, and we also don't want it to be above a certain numbers.

  • We're gonna set the maximum two just a 200.

  • No one's over 200 years old.

  • So now if we say bet and come over here and we try to decrease this.

  • You see that one is the lowest we could go.

  • For example, I enter negative 10.

  • Make sure all the required fields air entered and hit.

  • Submit.

  • Let's make sure this is actually email, and now we click submit.

  • You'll see.

  • That says the value must be greater than 10.

  • But let's say we put 201 here.

  • Click submit.

  • You'll see that it must be less than 200 so these validations are built into the browser.

  • Another interesting thing we can do is what's called step and by default the step is going to be equal.

  • The one so that means when we click are up and down a Rockies.

  • It's going to increase and decrease by one.

  • But for example, we could change us.

  • What if we wanted to to be by five.

  • So if we say this now, when I click up, it's going to increase by five at a time.

  • As you can see, the next input element we want to cover is going to be the date and put element, and again, this is going to be just like all of our other employees elements we're going to have a label which we're going to set a four to.

  • In our case, we're gonna say for date and we're just gonna come in here and we're gonna say this is going to be a birthday, for example.

  • So now let's go down here.

  • We're gonna create an input and is going to have a type, and we just want to set this to date to specify it's a date.

  • Gonna make sure he said the name to date the i d two dates that we match our four for our label over here, and we're just gonna close that off.

  • And now if we save you said that we get a date input, which already has some defaults force as well as a nice little date picker.

  • We can select what date we want inside of here, just like with the number field weaken, for example, specify a minimum and a maximum of three want.

  • So let's come in here, specify minimum, which is there's going to be 2019 June 10th would save that and come in here.

  • And if I try to select the date before that, it won't let me.

  • But it allowed me to select the day after that.

  • And if I come in here and manually change this, for example, I want to change it to the first.

  • And I tried to submit it.

  • After making sure all the other fields are submitted properly.

  • Make sure all the required ones are click submit.

  • You see that the value must be greater than that date that I specified as the minimum.

  • And again we could do maximum if we want.

  • But we already did that with numbers.

  • So we're gonna skip that.

  • Now Let's move on to the next type of input, which is going to be completely different than all the types you've covered so far.

  • This is going to be the check box element.

  • So it's credited ve inside of this day.

  • If we're just going to specify a little bit of text, this is gonna be outside of a label.

  • And this is just gonna be for favorite food because we want check boxes for all of our different favorite foods.

  • And now inside of here.

  • We're going to put all of our different check boxes.

  • We're gonna do each one of these inside of their own div.

  • So, firstly, if we're gonna have a label which we need for all of our different elements.

  • And they have a four.

  • And this first option that we wanna have is going to be banana.

  • Make sure you close that off, put in banana here, and then we actually want to create the input for that.

  • So this input is going to be a type here of check box.

  • We're gonna specify here our name, which is just going to be banana.

  • And we're going to specify lastly the I D.

  • Which is again going to be a banana.

  • Close it off and save it.

  • You see, we get a check box here for banana.

  • Now let's say we want to add another favorite food.

  • Let's just coffee.

  • That's DIV so that we can do the exact same thing.

  • But we just want to change this.

  • So we're gonna change this to be Apple changed the text to be apple pie could spell properly and make sure, of course, we update our name and our I d to be properly set.

  • Now if we say that you said that we have banana and apple and we can check box both of this if we want or just one or none of them.

  • But what if we only want to be able to allow a user check one box at a time instead of all of the boxes cause with check boxes, you can check as many or as little of the boxes as you want.

  • But if you want to allow them only to specify one favorite food, we need to use what's called a radio button.

  • So let's go down here and we're gonna create another def.

  • And inside this one, we're going to ask the user for gender because they could only have one gender.

  • We're going to use a radio button for that.

  • So let's close this off here.

  • We're gonna create our div, get a label inside of it, and this label is gonna be for our 1st 1 which is going to be male, and we can just say male.

  • And then we need to create our input.

  • So the input is going to have a type here which is going to be radio.

  • And now you would be thinking that the name here should be male.

  • That's actually incorrect because these radio buttons all need to share the exact same name, which is how we know that there can only be one selected.

  • So in our case is actually going to be gender for a name.

  • And we want to also put in here our i D, which is male, which again corresponds with our four element far label.

  • Now it's a bit, and you see we get a radio button for a male.

  • What's at the exact same thing for a female?

  • What's copy this down?

  • Make sure that everything's indented properly and this is going to be female female.

  • And lastly, our I d is going to be female now if we say that and the quick mail, that's all good.

  • But if we click female, you see it once Lex male and selects female force.

  • And that's because we share the exact same name of gender between both our mail label and our female label inputs.

  • Down here, you see, they have the exact same name, which means that we could only select one of them at a single time, as opposed to beos like multiple.

  • One thing that you need to do additionally with radio buttons, though, is actually specify a value so When you submit it to your form, you know what you're getting back.

  • So in our case, we want to put a value here, which is going to be male.

  • And for a female who want to put a value, which is going to be female now, I've saved that and actually enter instant information for a form so that we have some valid information.

  • And let's select mail and we click submit and you'll see we get gender here set to mail, which corresponds with the value that we selected here for a radio button.

  • Now the next element that we want to look at is actually not input element.

  • It's actually a select element, but it works very similarly to the other input elements.

  • What's going on here A little ways before a password and we want to come in here with R div and inside of this instead of an input.

  • Like I said, we're going to be using us select.

  • And of course, we still want a label or select.

  • So we're gonna make sure our label is specified here and inside of our select.

  • We're gonna select eye color so we can just say here are eye color, and we want our level to say that I color and inside of the select.

  • What we're gonna do is we're actually gonna put options inside of here.

  • So to do that, we come down here, we type in option, and our option is goingto have value value.

  • So in our case, we're gonna put green as the value.

  • And it's also going to have that in here as the actual text, so we can just put that here.

  • You could also use what's called the label section and specified green here.

  • So we'll do that on another option was copy this down.

  • We're gonna do a read option, so we'll say red.

  • But instead of putting our value inside, the option like that would just say our label here is going to be read and also on our select.

  • We need, of course, make sure we have a name here which we're just going to set to eye color and again our i d.

  • Which is going to correspond with that label for that we have.

  • So I d here is going to be eye color Now what say that and you can see we have our eye color selector, switch sides green and red.

  • Are there two different values that we can select from and again?

  • We have green specified inside the option for the label, or we can use the label attribute of our option.

  • Either way allows us to set the actual value that we have seen inside of the select here.

  • Now, by default, a select only allows you to select one option.

  • But you can come in here and you can adjust the attributes which is called multiple.

  • And if we say that we can now select multiple values by just holding down control and clicking on them like this.

  • And now we have both of our values selected, and if we fill in everything else on our form just so we can see what this looks like and makes our passwords that and we submit, you see that has eye color showing up twice a bull for green and for red because we selected both of those values from our previous form.

  • Now, if we go back to our form, you may notice that we don't have any form elements that allow us to specify watts of text and text with new lines in them and et cetera.

  • So in order to do that, we're going to use another element rapid instead of motive here.

  • And this one is called a text area, and this again is not actually from an input.

  • It's its own element called text Area, and this is one of the few elements that has an actual closing tag because the value actually goes inside of the text area here, as opposed to on a value attribute.

  • So now it's first before we get carried away, put a label which is going to have four here, and this is going to be a file for a user.

  • We could just say bio and again, we want to make sure we have an I D on here, which is going to match the four from our label and a name so we can actually use this instead of our form and not to put a bio inside of here.

  • So we're gonna say my name is Kyle, and if we say that, you see that we have a bunch of extra space being added into our element here, and that's because this text area converts all of the white space.

  • So all this white space before that my name is Kyle in this new line that we have here, all that's being added in.

  • So if you have a default value for your text area, you want to make sure there's no white space between the opening and closing tags.

  • Now, if we say this use, that is properly not adding extra space force, which is what we want.

  • But let's just remove that default value for now.

  • And we say that you see, we can enter in our values we want, but we can also hit, enter and do that and enter in multiple lines of text very easily.

  • And we can even expand this if we need to to make it larger toe work with Now Select and Text Area are the only two anomaly type of elements that don't actually use the input for their element.

  • So now let's go back to the input element to talk about another really interesting element, which is called the hidden input element.

  • We don't even need to create a different this because it's actually just completely hidden.

  • We won't even need a label.

  • We just need input you specify the type of hidden and that's it.

  • We're gonna give it a name.

  • So pressure say, this is going to be called hidden, and we're going to give it here of value, and we're just gonna set the value too high.

  • Now, if we say that, you see, nothing actually shows up on our form.

  • But if we do submit this form, so let's make sure we submit it with all of our different required values and click submit.

  • You see, we get that hidden attributes being passed along with the value we specified inside of our HTML.

  • But it doesn't actually show up on our page, which is why it's a hidden input and usually are not able to interact at all with hidden inputs, which is exactly what they're used for.

  • The really great when you're trying to do some fancy manipulation in Java script where you want to send something down from your server.

  • But if you're just creating a generic HTML form like this, the hidden attribute is almost never going to be useful.

  • Much you're doing something fancy and JavaScript or on your server, the next interesting element that we have to take a look at is actually going to be the file input.

  • So it's creative so we can wrap this in here.

  • What's created label for it first, This is just going to be for well, just call file and we could just specified as file.

  • Then we're gonna have our input tag.

  • We want to say that this is going to be an I d.

  • Of file.

  • Just so it has the same czar label.

  • The type here is going to be a file type, and again, we're just going to give it a name of files so we can know what it's actually looking for when we submit our form.

  • And now you can see down here we can actually choose a file to submit to our browser, but the first thing we need to do to make it so that when we submit this to a server that it's actually useful for our server.

  • We need to specify a new attributes on our form here, and this is going to be called the E N C type, just like this and inside to hear what we want is multi part formed at it on what this essentially says, his files are very large.

  • We can't send them all at once.

  • It's going to send that file in multiple parts.

  • So we need to tell our server that we're sending in multi parts are formed it into multiple, different sections.

  • So that knows to look for multiple sections, oven upload as opposed to just one single form upload, and this will actually allow it to consume it.

  • Our files to our servers.

  • So now let's test that out was coming here.

  • Choose a file.

  • Just choose one of my channel arts.

  • Type this in here and make sure we have all the different information for our forms submitted.

  • And if we click submit, you'll see our file is showing up is our channel art one dot PNG, which is what we selected when we uploaded our file.

  • Next, let's kind of try to rapid fire here.

  • There's some of the lesser used input elements.

  • Let's make sure we create our div.

  • And inside of this did we want to make sure we have our label and this is going to be for a phone.

  • We're gonna ask our user for their phone number so we'll say for a phone gonna say phone is the label and we just want input.

  • And the actual type of this is going to be tell T e l just like this stands for telephone name.

  • Here is going to be phone books.

  • Phone cannot type two days in bed, phone and an I D.

  • Which is going to match our four of phone.

  • Close that off.

  • And there we have our phone field on.

  • The really great thing about this phone field is that if a user's on a phone entering this information, it'll give them a nice keypad, which is specifically meant for entering phone numbers and not just for entering generic text.

  • Next, we're gonna have another input here, which is going to be for girls.

  • So let's create a div cried our label for it, which is just going to say for a girl, close the soft girl and we want to make sure we have our input, which is going to have a type here of euro, a name of euro, and it's going to have an I d of your own.

  • Close that off.

  • There we go and you can see we get our your l entering down here and another great thing about this again on mobile phones.

  • It's going to give them a special keep ed that specifically meant for entering you are else.

  • Lastly, I want to cover just the kind of fun input element here, which you probably won't ever see anywhere.

  • But it's just fun to use.

  • Every once in a while this was going to be for color.

  • So we're going to say here for color, make sure it says color here and again, as you guessed, it is going to be an input, which is going to have a type of color lips type color unnamed, which is going to be color.

  • And it's going to have an i D.

  • Which is color.

  • Close this off and now you see, here we have a color selector.

  • We can select any color that we want and click okay, and it'll actually select that color for us.

  • Let's say we wanna have this blue color click, okay, and it changes it to blue.

  • And finally, after that long video, we've covered everything you need to know about HTML forms.

  • Make sure to click over here for more videos where I simplify the Web for you and subscribe to the channel for more videos just like this.

  • Thank you very much for watching and have a good day.

information is key, which is why forms are one of the most important parts of a website.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it