PHP Tutorial (& MySQL) #19 - Basic Form Validation (part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
or rather than gang so in this video I'd like to talk a little bit about form validation but what do I mean by that well by form validation I mean we need to validate the data that a user enters into this form after they click Submit because if you think about it a user could just type in a name here not an email they could leave this blank and leave this blank click Submit and then we've got that data we need to say hey we want to check this data is it the correct type and have you actually entered in something at all so that's what we're going to do now we could use html5 validation on the front end such as using the required attribute on our input fields or by using input types and that's always a good idea to do as well but in this lesson I want to focus on server-side validation using PHP so once the form is submitted the data is sent to the server the file runs and the data from the form is validated in PHP code up here now this is always a good step to take as well as front-end validation to make sure that the data you use in your app or save to a database is correct and it's the expected data okay so we do receive an email when we want one we do receive ingredients etc so then first of all we're checking if the form has been submitted and we have data that's the first step right and then in here at the minute we're just echoing this stuff to the browser and we're escaping any kind of malicious code using this function right here HTML special chars now the first thing I'd like to do is check that all of these fields right here and not empty they all have to have something in them okay so we need to see if those are empty what I'm going to do is just comment this stuff out for a minute and we're going to do several different if checks one for each form field so I'm gonna say check email first of all that's a little comment so we know what everything is as we do it and here I want to do an if Jack and I'm going to use a function called empty right and then I'm gonna pass in to this function dollar sign underscore post and I want to get the email field so I'm checking if this field is empty if it's empty it means there's nothing in it the users not typed anything and if that's the case we want to echo some kind of error because they have to type something so we'll echo an email is required and then we'll also echo a br tag as well okay so we're checking if the email is empty if it is then we're going to echo this error saying it should quiet else this means it's not empty and it's fine so in this case we're just going to echo what the user types in for the email so let's cut that from there and paste it down here does that make sense if it's empty then we want to send back an error if it's not empty then that's fine and we'll just send back what they typed in just this confirmation for now so we'll do this kind of check for each different field so I'm going to copy that paste it a couple more times we're going to check the title right here and we need to check the title field over here and then we'll change this to a title is required oops like so and otherwise if it has been entered and it's not empty we'll just echo out the title from the post array and then finally we want the ingredients and we'll say at least one ingredient is required if it's empty and if it's not empty then we'll just echo out those ingredients instead just for confirmation okay so we'll just do a little comment right at the end as well let me delete these we don't need those at the top anymore we're gonna do a little comment right here just so we know that this is the end of the post check right here okay so then I'm gonna save that and I'm gonna give this a whirl and hopefully it should work now if I submit without entering any information we can see an email is required a title is required at least one ingredient is required so we'll enter in an email or say Yoshi at the net ninja Cody at UK then submit and now we just get these two errors but we also get the value right here so this is valid we've successfully entered something in it might not be fully funded we've not checked that it's actually an email and we could just as easily type Yoshi here submit it and that would say yeah it's valid but at least it has a value whereas these two don't if I type in the piece of title just call it title then you can see an emails required we get title and then at least one ingredient is required we can do the same thing here in fact what I'm going to do is just a load of stuff like that submit it all and we don't get any areas we just get a combined load of junk which is all of the values together okay so that's the first step of our basic validation we've checked that those fields have actually got something in them but we need to do a bit more we need to check that this is actually an email okay and not just some random string of letters we need to check maybe that this is a title may be a bit looser validation on that but we need to check that this is a comma-separated list of ingredients and that could be tricky as well so what we're going to do is use filters and a bit more validation in the next tutorial to do that and I want to warn you I am going to be using a bit of reg X to do this now you don't need to know reg X I'm gonna give you the regex codes to verify these different things but if you want to learn about it I do have a whole tutorial series on reg X on this a very channel so the link will be down below in this video and the next video as well so you can check that out there
Info
Channel: The Net Ninja
Views: 63,558
Rating: undefined out of 5
Keywords: php, tutorial, php tutorial, php tutorial for beginners, mysql, mysql tutorial, mysql tutorial for beginners, sql, sql tutorial, php for beginners, learn php, php mysql, php and mysql, form validation, php validation, php isempty, is empty, isempty, empty, empty()
Id: g7x4JO0YW1s
Channel Id: undefined
Length: 5min 58sec (358 seconds)
Published: Mon Feb 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.