Sending our Form out
Creating tags is fun but it's pointless unless we have some use for it. In this video we will introduce the form tag and see it in action.
Video Transcript
00:00So I want to show you first, how do you build out that form so when you do build a form
00:04and if you're communicating with a developer, that you'll know what questions to ask them.
00:08So first of all, our biggest problem right now is what would happen if we would have
00:12two forms?
00:13What if the user was in one form and password was in another form?
00:17There is really no way for us to differentiate and know when we click on the submit button,
00:21what are we actually trying to submit?
00:24To be able to solve that problem, what we want to do is we want to go back into our
00:29code and this time around we want to actually grab our code and I'm going to
00:33push that code a little bit, indent it a little bit in because I want to add a new
00:36tag which is called the form tag. Now the form tag will basically cluster
00:41everything that we want to be part of our form. Now don't worry if you have
00:46things that are not form related such as an h1 or a paragraph or something in
00:50between your forms. It will completely ignore them. It will only look at stuff
00:54that are connected to inputs so it's going to add in the different inputs and
00:59for example it will ignore the label but it won't ignore the input. Now if you
01:04I want to just a side note for whoever is watching this that variable names, there
01:10are variables. Now we didn't really learn about variables because that's
01:13something that our JavaScript developers or any of our students in any
01:17development course would know but I want to say a side note that the input
01:20information for example in this case the variable name would be user. You
01:24could define the variable name also using the name attribute so you could
01:28type in the name attribute and give a name to your variables. So in this case
01:32I want to name the value that the user sends inside so this value we could call
01:36it user as well. So by that we set the name to the information that we'll send
01:41on afterwards to the service. Let's say we want for our input for our password
01:46let's give it a name as well so let's say the name itself for the variable
01:49that we want to send we want to call it P. Just for the sake of this
01:52example P for password and how about we'll do U for user. Alright so we have
01:57here this two different forms of information. The next stage now that we
02:02have variable names to our elements we know because the server needs to know
02:06what those variable names are so it's or that you would tell your server side
02:10developer these are the variables that I'm sending out or on the other way
02:13around you would get a specification from the developer that build it or the
02:17third party that build it what are the variables that they are expecting for
02:21and you would put them inside of the names of your elements. Now that
02:25everything has a name and everything has a sub and now we have a submit button
02:29it's time for us to tell our form where should it submit to. To do that we're
02:33going to use the action variable the action property or attribute and I'm
02:39going to for now just say I'm going to keep it empty. Keeping it empty means I
02:42want to send it inside myself and by doing that if I click on save and I
02:46go back into my form and I go back into my page and I'm going to click
02:50on refresh here for a second and I'm going to put a username here I'm going
02:53to call it zero to geek and I'm going to put a password of 1234. When I click on
03:00the login button notice what will happen it's going to ask me do I want to save
03:03the password no not right now and when I submit it notice what happens on the
03:07top of my URL because I'm sending the information back to myself it's
03:12basically appending the information in this URL parameters where you see that
03:16you was our user was the variable that we created and the value was zero to geek
03:21and in our P which was our variable P if we look at it just for a second was our
03:26password exactly so we have your user and our password now the least safe way
03:30of sending information is the default way which is called the get way now
03:35you probably would want to change the way of working with it now I want to
03:40show you how to set that default again by setting the type of method so the
03:44method of sending information right now is called get that's the way we're
03:48sending the information so he literally if I go back into my form and click
03:52refresh again even with that inside and I'm going to change the username to Ben
03:56and the password to one and I'm going to click on login and I'm not going to
04:00save this information you'll see that we're using the get and it's
04:03updating the information in the top of URL if we were working on a server side
04:07code this would be really perfect because then it would fetch this
04:10information and do whatever it needs to do and update the experience to the
04:14user now one of the biggest things you could probably notice that it's very not
04:18safe if we're going to put here our information directly in the URL and we're
04:22putting the user's password directly in the URL anyone later on could then look
04:26at your browser history and see your username and password and obviously that
04:32wouldn't be a smart move and it wouldn't be right to do for your users
04:35as well so when you're using really with a username and password your best
04:39option is sending the information in a transparent way where the information
04:43doesn't get written on top of the URL beyond that when you're typing it and
04:48again there's a maximum length of URLs so there's a maximum amount of
04:52information you can actually send in the URL you can't send also images you
04:56can't send a lot of different things that you could when you're posting it
04:59with the post instead of again so I want you I just want to show you how
05:04you would do that I'm going to go into the URL I'm going to change my method
05:07to post this time around when I change it to post and I click on save
05:12if I go into my browser and I change my username again and this time around
05:16let's call it zero to geek and I'll put type of a password 123 and I click on
05:21login notice what happens we're not gonna save it oh we got here a post
05:26make sure that oh I didn't refresh I didn't refresh there we go so let me
05:31just delete everything here we're gonna click on refresh and we're gonna try
05:35this one more time so I'm gonna put your zero to geek and a password is
05:38gonna be 123 and I'm gonna click on login and notice what happens now
05:42although it does send the information it doesn't show it up on the top at all so
05:47there's the two ways of sending it now it's not your complete choice if you're
05:51gonna send it in the post or again that really depends on your developer many
05:55times because if you're working with a third party solution that third party
05:59solution comes with a documentation that tells you are you allowed to use a
06:02post are you allowed to get or could you pick among the two some services do
06:06one or the other and some services do all options all right so that's all I
06:10really had to say about the the building the basic form and how to send
06:14it and I want to I want to look at it just for one more time together for a
06:17second and the one we saw the action is really where we want to send our
06:21information to in our case we left it empty because we don't have a server
06:25side developer working on our application so we don't have something ready for us
06:29to take really a username and password but in a real case that
06:32application we put a URL in here in our method we had to choose between a post
06:36or again we have to two inputs in our information one is called you as its
06:43URL for the URL or the variable that we're sending out and one is called P
06:47for the variable we're sending out and then we have a submit button that
06:51enables us to whenever the user clicks on it to submit the form itself now
06:56before we finish up I want to comment this up and I want to show you a live
07:00example of a live form that is actually our real form that we use I'm just going
07:04to copy it here for a second bear with me I'm going to copy this form that we are
07:08actually using for our newsletter so this is a form that we're using with a
07:12third-party company so the third-party company is Mailchimp and enables us to
07:18basically have a newsletter that enables us to basically send information
07:21to our users now there's a lot of extra information here that we're not
07:24going to get into at this stage but I want you to know what the most
07:27important parts first is that the way they send the information is using a post
07:31right it's unseen by the user it's not saying that's kept in the history of the
07:37browsing of the user and the action itself has the URL where the for our
07:42submission form our post submission form is managed and you can see that if you
07:47would add and we'll look at it in the cycle once we finished seeing this
07:50overview of the class you see so that's the information in the form the
07:54next element you'll see is we have a label just as we had before and it's a
07:57link to our input our email input and you'll see that we have your input of
08:03type email which we haven't seen yet but an email basically enables you to do
08:06email validation so it's a specific input type for emails all right there's a
08:12lot more information here and we're not going to dig into it but basically you
08:15could see the basic formation of a form you could see inside of your in a
08:19more advanced form and how it a more advanced form would work if I save
08:22this and I go to my browser and I click on refresh you'll see that we don't want
08:27to send any information now we'll send it again we're refreshing so you can see
08:32that join our newsletter email address and expecting us to write an email here
08:36and once you click on subscribe you will be automatically subscribed into our
08:39newsletter which I recommend that you do join our newsletter because you could get
08:43there a lot of cool stuff but you have to type first an email inside but they
08:50should first put it their email and then click on the subscribe all right so see
08:55you in our next section or set next video
