PHP Form Validation for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys welcome to another video and in today's video we are going to learn a bit about form validation with PHP so if you ever been like building these kind of forms in just HTML and CSS and you are just like curious where is these values go what is this data going so I'm going to show you what you can do with it within PHP and how you can validate it I'm not going to cover how to access databases and how can you send the values to the databases this is going to be for another video in here I'm just going to show how you can validate the data and how you can grab these values okay I'm just show you in here what we are doing so we have in here a log in register form by the way that you asked your user to put a username in a password so for example if I go in here and I just press register look it's going to give us an error saying please insert your username another one please insert your password if I press just like two or three letters I press register and it says your username needs to have a minimum of six letters I'm gonna do something like that as well for the password and now it says your password needs to have a minimum length of six okay so imagine that now I put something else that is all good I'm gonna press register and it says thank you for registering with us hi guys let's get started with this so I'm going in here to my visual studio code by the way I didn't mention this because we are using PHP and this is a server-side language for you to be able to see these on the browser if you never touch with PHP okay you will need to install something like this this virtual server called zamp there are and some other ones but this is the one that I'm gonna be using it's available for Windows Linux and Mac OS okay so just download it if you want to follow with me once you download this and install it you might have something like this I'm just gonna show you look icon drop panel and how can you access this control panel let me just show you in here I'm just gonna go in here into Sam this is where I installed okay this is the the place where I installed in here there is this part that says amp control this is where you installed it okay you double-click on this amp control and it's gonna open this control panel make sure that you start the Apache and MySQL okay both of them so you can you can see what you are gonna do now so after you need to start your project inside of these htdocs okay in here you're gonna create a folder this is the one that I just created for evaluation v2 okay and in here this is the code that you're gonna put okay so in here Visual Studio code I'm going to create a new file I'm gonna save it as index dot PHP okay inside of this form validation v2 that I have I'm gonna save it and I'm gonna do html5 okay this is the shortcode for all the code that we need I'm just gonna write in here at h1 same test just make sure that all is working fine I'm going in here now and what I'm gonna do in here is I'm gonna do localhost okay I'm gonna press for validation I need to put the v2 okay and there it is look at the top I'm gonna do a bit of zoom so you guys can see better test okay so it's all working fine so you need to go and here at the top on the URL localhost for slash the name of the folder that you created in here into the htdocs okay alright guys so this is now covered so what I'm gonna do is just copy some code that I have in here I'm gonna put in here at the top first of all I have a link let me see if something is missing in here okay this one was cut off so I got the link with the stylesheet for some bootstrap if I wanted to use I got a link to a main dot CSS which I'm going to create now I'm just gonna put in here PHP form validation okay I'm going to create a new file save as main dot CSS this is where all my CSS is going I'm not going to be by like doing everything step by step in here from the CSS because this is a tutorial about PHP okay but I'll scroll down just like slowly so you can if you want to copy just pause it the video and then of course you can you can copy the codes that I have okay just gonna scroll down slowly slowly slowly and you can copy just post the video I'm also gonna put the links in the description by the way so if you want to copy all right so we got all of these in here now let's go in here and start creating some things inside of the body I'm going to press alt Z so I can basically wrap all this code and write okay let me just go in here close these up I'm gonna do a bit indentation right so we have a div with a class of container from bootstrap to make sure everything is nicely centered on the page we have a deal with a class of card which I'm gonna style win here with the CSS and then I have a like a user image okay like an avatar let's see how does it look like at the moment okay I'm just reset the zoom and this is what I have so far right okay in here inside of this deep with a glass container I want to place my form I think it's just in here okay I'm gonna press my form look now I have a form I'm gonna put in here an input I'm gonna put in here another input this one is gonna be password I'm gonna put in here placeholder okay and the placeholder is going to say user name and the placeholder in here is going to say password okay let's see how does it look like let's refresh and there it is okay we got a user name we've got a password this is all styled up because of this CSS that I put in here if you remember right so we got this one and now let's start talking about PHP ah right love I'm just missing in here a button I'm going to put an input of type submit' okay because you need to submit and the value is gonna be register okay just gonna put what was the class I have a class in here a register button okay let's go in here let's refresh and that's fine okay here is our form now okay let's go in here on the form action this is where the PHP starts when you are creating a form you need to send these data somewhere usually what what people they do is like you can send is that data like to the same file to this one or you can send this data to another file which will handle all of these and let me just show you okay I'm gonna create in here that the action that I want is going to be for example a file called results dot PHP okay so whenever we press these submit button it's gonna grab all these values and it's gonna send them in here to the results dot PHP let's create this file in here new file results dot PHP okay there is nothing in here as you can see now okay what I want to do is also a method okay method and the first one that I will do in here is do this one called get and let me show you what is this so if I go on results dot PHP to start a bit of code in PHP you start it like this okay this is how you start the first line and this is how you close it so whatever goes in here in between is going to be PHP code if you want to start the variable in PHP you could put something like this dollar sign and then username and then I'm going to say that this is going to be equals to and here it is when you are doing this method of get let me just show you in here I'm gonna put name name of user underscore name for example and the password is gonna be name equals user password okay let's go in here let's refresh I'm just gonna type down something in here I'm gonna press register and look if you see in here at the top look we got the results dot PHP the file that is submitting this form and then it has in here a question mark and then it says username equals to asdasd let me just like redo this again let me say I'm gonna put in here username telmo and the password is gonna be one two three four I'm gonna press register and look in here at the top question mark after our results dot PHP we got a question mark and it says username equals to telmo and user password equals to one two three four okay so this is our method that we have in here called get whenever you use this method all these variables that come from these name and these name in here from the username and the user password okay they come as variables in here at the top in the URL so as you can see this is not a good idea if you want to use some kind of sensitive data like passwords this would be ideal for example if you are using blog ID posts or if you want to pass in here for example a color or something like this and then you're gonna grab it in the next page and you can do something with it but let me just show you in here now results dot PHP we could do something like this username equals and then there is a super variable that's how is it called in a PHP call to get and we are gonna get what this variable that comes in here from the name that's why you use the name in these inputs and the name that we put in here was username okay I'm gonna put in your username and then I'm gonna create another variable called password equals to okay I'm gonna put get this is like I said a super variable from PHP that lets you grab these things that are being passed from the forms and what is the name in here the name is user password okay I'm gonna put it in here and look if I want now to actually place these values on the page I could do something like these in here let's say that I want to start another html5 this is a regular HTML page look in hitting the body I could do something like these underscore PHP I'm just gonna start in here the h1 okay h1 and then I'm gonna close the h1 as well and then in here in between I'm gonna do echo and then this variable username okay don't forget to close the PHP okay so like this so you open PHP like these and you close it like this this is how you will inject PHP into a regular HTML page I'm gonna do the same now so I'm gonna put him here before these username okay some space and then we can put that then I'm gonna go in here and I'm gonna put password and then echo this is when you say echo is like print on the page all right I'm gonna go in here grab this variable password okay let's see how does this look like now I'm going back to the main page in here I'm just gonna put telmo and I'm gonna put the password like zero nine eight seven six whatever I'm gonna press register and look username telmo user name zero nine eight seven six and it's been passed in here at the top as well now like I said this is not the best way for you to to pass sensitive data okay whenever you want to for example pass some sensitive data and pass it to the database you should actually use a method called post okay so there is these two methods that is the get and there is the post and let's see how does it look like this one if I go back let me just go back in here I'm gonna press my last name for example sampaio and I'm gonna put the password zero one two three four five six register and look now there is nothing in here in the URL at the top and we got some errors in here that's because now if you want to grab these values in here first of all it's not going to be this super variable called get but it's going to be the super variable called post okay this is to grab values that they are coming from a form with a method of post all right so let's retry this again just gonna put some piyo and I'm gonna put 0 1 2 3 4 5 whatever and submit there it is ok guys so this is working completely fine now you know the difference between the get method and the post method like I said if you want to pass some values for the database use the post if you have sensitive data use post if you just want to have some like IDs for some blog posts or you just want to have some kind of color or something some variables that you don't mind to be passed in in the URL you can use get alright so we got this part in here done now let's actually start validating whatever we have in here so what we can start doing is going in here into our results dot PHP and just in here under we could actually go and do a if statement that says something like this if the username that we have okay there is a PHP function called empty and this empty function is going to see if this variable that we have in here at the top is empty or not is like empty string or there is nothing in there so if it's empty okay what we are going to do is we're going to create a variable in here called name error and this name error is going to be equals to please insert your username okay and we are gonna do the same for the password as well let's go in here and I'm gonna do password okay this is the variable that we have in here at the top and we are going to create in here another variable called the password password error please insert your password all right so we created in here too variables if this value in here is gonna be empty now what also we can do is just like get rid of this code and just go in here and we can actually include okay we can include our index dot PHP because now we don't have to repeat all this code that we have in here whenever we go to the results page it's gonna have this this form again let's go back in here and test this out okay I'm gonna put in here something like these Salmo I'm gonna put a random password the register and that's fine as you can see look we are on the results page now and nothing happened okay let's seem here when we created these errors we can actually now pass them to our index dot PHP in here so I'm here under the input I'm gonna put a break okay I'm gonna start a PHP code in here so this PHP code is gonna say if the if is set okay what is that what does is set means it means that a variable is set is already been declared and has some value okay and this is what comes from here from the name error this is only gonna be initialized if the user name is going to be completely empty same for the password so I could put if is set name error and then I'm gonna put in here this one I'm gonna end my PHP I'm gonna put paragraph and I'm gonna close the paragraph and now the PHP code goes in here so PHP then I'm gonna do echo to print my message and the message that I want is going to be error okay let's close PHP and we caught to close our if look we got our if in here initialized at the top with this part in here of the curly brace we need to actually close it as well curly brace and finish the PHP code if this one is working fine we can actually just go to the password after let's get rid of these let's go in here I'm gonna press register and look please insert your username okay as you can see look this is not in here but because we are running these from the results dot PHP we got all this coding here at the top first so it says username is going to be equals to whatever we grab and password the same if it's empty the username it means that if if nothing comes from here we start this variable name error please insert your username and we start another error if the the password is also empty and then we include the index dot PHP where it's gonna continue all the code so let's do exactly the same in here now I'm gonna put in here another break tag and just in here we got in your password error password error let's try this out again let's go to the main page just gonna press register please insert your username please insert your password so all these is great now we can actually go and set up some else if in here else if and this is going to be if for example the STR land okay of the username so this is the string length if in here for example you are writing down your username if the length is going to be less than six imagine that you just want the minimum username to be with four letters or five letters or six this is what you were gonna do if the string length of the username is going to be less than six we could actually do name error okay this means that this is not empty if this is not empty this code is never gonna run so we are going here to the name error is going to be equals to just put single quotes and I'm gonna put your username needs to have a minimum of six letters it's gonna be something similar I could go in here and just copy this out in here for the password now and the password as well if is less than six and you could just write something like this let me go in here just copy paste your password needs to have a minimum length of six all right so let's see how does it look like so far let's go in here I'm just going to put one two three or one and your password needs to have a minimum length of six and we still don't see anything in here in the password let's see what's missing because in here we put name error we don't want name error we want password error okay let's go back let's go back to the main page let's - I'm gonna put another - and your username needs to have a minimum of six letters your password needs to have a minimum length of six now look at this imagine that for some reason when you press register you don't want to the values that you have in here to be gone - to type them again let's see that you want to give them the option to actually you know remember I don't want this if I press these and then these are the to clear it out I want to remember the values we could go on the main page index dot PHP in our input you could put the value equals two and now this value is going to be a PHP so PHP and let me just start this one and we're gonna do echo and now we are going to use a special function from PHP which will help you get rid of any quotes or any things that are coming like trying to make a SQL injection for example if they are trying to access your database put some kind of malicious code these HTML special chars is a special function from PHP which will make sure that users that will type down something in here into the input is going to be all safe the code for you to use all right so this is a very special function and whatever we want to pass in here is going to be the variable the variable called user name okay let's close our PHP and let's try this out again let's go to the main page okay I'm just gonna write down these I'm just gonna do these and there it is look now it remembers it let's go back again I'm going to put I'm just delete this I'm just gonna do for example TAS and I'm gonna write on something and look it remember the username alright so you could do now exactly the same thing on the password and now let me just show you something else on results dot PHP there is another thing that will help you protect against malicious code that someone will try to put on your form just to make sure that people are submitting code because as you as I told you we are in hearing the index dot PHP initially in this form we submit our code from here to the result dot PHP and then it's being handled him here but imagine if someone is submitting code to these results dot PHP page with some malicious code or whatever you can use in here on the top instead of for example this password equals to post ok there is actually another way and this way would be look password equals to filter input and now in here you want to say what kind of you are going to filter the input make sure that is being submitted from your index page make sure that it's all the code is safe for you to use so this function in here from PHP will let you put in here first of all if is an input get or post so is an input post that you are expecting and the second one is going to be the name of the of the input and the name of the input that we have in here for the username is look user underscore name just like what we have in here at the top actually this one we want to put the password okay and we could actually do the same I'm just put in here this one we could actually do the same for for the username so I'm gonna put in your username ok let's just comment this code because we don't need it anymore let's make sure that we are using the username in here let's just refresh the page cancel let's go in here let's get rid of this and if I press something like this BAM it's working fine okay it's all working fine now if you noticed look if I go back in here all the time that we go back is getting all these kind of err in here what we can do is because when you are loading just this page just the index page alright you don't actually have access to this username in here because on the results page when you are right right here on the index dot PHP you are putting all this code forward submitting this call to the results lot PHP that's when this variable is getting cold and is getting set okay but when you are loading this page just for the first time by yourself you need to do some kind of thing to to cover to cover it so what we will do in here at the top is start a PHP code and what we will do is if okay is set or you want to say is not set so we're gonna put in here this exclamation mark before so it's like the negation so if he's not set the username okay we are going to set the user name to be equals to an empty string alright so let's try this out I'm just gonna put like three letters something in here register and this is fine let's go back now to the index page and look you don't have this error anymore because we just covered it in here at the top if the username is not set nothing is coming through because this the initial load of the index page let's set up the username equals to empty string and then when we go in here to our input with a username the value is gonna be no so there is no where'er in there okay there is only one thing in here missing now you could actually look if you want to put for example a confirmation make sure that the two passwords that you are passing down are exactly the same you could go and get into the results get one value for pass password warned and then another one for the password too and then just do like a if statement saying something like password one equals two password to something like this and just make sure that both of them they are correct if not you could create another error like in here all right so this is it in here so let's just now do the confirmation so what we can do in here with the confirmation is if okay if the is empty okay if is empty our name error and is empty our password error it means that we never got to set them up I want to include a file called results dot PHP we don't have this file but we will create it now else it means there are some errors I will include my index dot PHP all right so let's create these results dot PHP I actually have it in here so I'm just gonna copy this code so the results dot PHP not this one we got this success okay this is what I want I want search SAS and let's copy this code I'm gonna take this out create this file says dot php' I'm gonna put all this code in here and look this is what this code is doing basically I have a container with a card with an image and then just with the registering US thank you for registering us let's go in here let's refresh and I'm gonna put Salim oh I'm gonna put the password 0 1 2 3 4 5 6 click the register and something is wrong okay what else we missed in here let's come here into our results dot PHP and see hmm let me just actually go back in here refresh the page a couple of times I think I was missing refresh the page I'm gonna press tell mo I'm gonna press 1 2 3 4 5 6 7 8 register something is wrong still your password ok because my password needs to have a minimum of 6 letters so I'm gonna put some pile preschool put a couple of letters registered and there it is now thank you for registering with us alright guys this is it for this video I hope now you can understand a little bit more how you can handle this data from forms with PHP and how you can try to validate them there are a lot of ways to validate this is just like scratching the surface because for sure there are much more secure ways to do this but this is just like a way to show you that you can do it as well so that's it guys I hope you like this video if you have any questions just put a comment down below and make sure to give it a like make sure you leave it a review and and that's it I'll see you in the next one
Info
Channel: Telmo Sampaio
Views: 40,268
Rating: undefined out of 5
Keywords: php tutorial for beginners, php tutorial, php form validation, php form validation tutorial, php form handling
Id: MMHUxT1pSMg
Channel Id: undefined
Length: 35min 10sec (2110 seconds)
Published: Wed Aug 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.