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

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

Basic form validation using PHP

Full Transcript

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

Original Description

Hey gang, in this video we'll preform the first step of some very basic form validation to check our input fields have not been left empty! REGEX Playlist: https://www.youtube.com/playlist?list=PL4cUxeGkcC9g6m_6Sld9Q4jzqdqHd2HiD ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: + Course files - https://github.com/iamshaunjp/php-mysql-tutorial + VS Code editor - https://code.visualstudio.com/ + Materialize Playlist - https://www.youtube.com/watch?v=gCZ3y6mQpW0&list=PL4cUxeGkcC9gGrbtvASEZSlFEYBnPkmff 🤑🤑 Donate + https://www.paypal.me/thenetninja 🎓🎓 Find me on Udemy + https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →