42: How to Display Error Messages Using PHP | PHP Tutorial | Learn PHP Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're gonna create error messages inside your website and is going to continue off the previous episode we're talked about error handlers inside PSP so if didn't watch the previous episode I recommend you guys go back watch it first and then return to this episode here so what we're gonna do today is I'm going to show you guys a form which is essentially a signup form inside a website and when the user submits the form depending on the error message that they might receive we're going to display it inside the website okay so as you guys can see in front of me here I have a very basic form that has a input set to first name then we have another input set to the last name email username and password essentially the information we need when we sign somebody up inside the website now when the user submits the form the information gets sent to a sign up that anchor PHP file which is the file that takes information from inside the form and does something with it okay and we're sending it using a post method so inside the signup form you guys can see that at the top here we have a basic check the checks if the user actually clicked the button when they enter this page because you can actually go inside the UL go into this signup the link to PHP file without actually clicking the button and run the code even though you you didn't actually click the form so we want to avoid the people actually signed himself up inside the database without actually having clicked the form so this is the first take that we're doing inside our signup form here now the second thing we're going to do is we're going to include the database connection and again we're not using a database in this example but if we were to have a database this is where we would actually connect to it and then we get the data from the form now down here we do something with the data such as checking if some of the inputs were left empty which is one of the error handlers we have down here we also make sure to take up some of the characters are not valid inside the the code here and we didn't do this one in the previous episodes if you guys want to have this preg match function that checks for characters just go and copy what I have here okay now we also have a check down here to check for a valid email so if your email address is not a real email address then it's going to throw an error message as well and then finally down here we have an else statement that sans the user up if there was no error messages inside the script here now I just want to mention that there's a lot more error handless we could use inside the script but I just want to demonstrate how we can use error handlers and how can get error messages inside our website so I'm not going to show you guys all the examples of all their handlers you might have inside a sign up script if you guys want to see more of these go ahead and take up my login system tutorial which is a few episodes from now ok so what I want to do and what I want you guys to notice is that when de is an error message I'm not simply echoing out you made a mistake I'm actually taking the use of back to the front page using a header function and then I'm closing off the script to make sure that no other else statements and checks and he actually continues because we just want to close off this entire script here and when the user goes back to the front page is going to go to index the PHP question mark sign up equal to empty at least for the first check here with the empty inputs they might have typed in now when it does that inside the index page we need to graph this data and then do something on the page depending on what it says inside this data here now what I want you guys to notice is that we could essentially just have deleted the signup equal to pardon to sat empty but by including the sign of equal to actually creating a get method inside our index page meaning that we can actually grab the data inside once we go back inside the index page using the same way as we did up here okay just using a dollar sign underscore get instead of post okay just gonna go and reset everything now again if you guys don't have the code here just go ahead and watch the previous episode and then copy the code from in there or you can go ahead and download the lesson files in the description of this video here okay so what I want to do now is I just want to show you guys how the form actually works inside the website but because they were were to go inside the website you guys can see I have to sign up from here I didn't mention that I did actually include a stylesheet inside my index page so that's why you can actually see that the website has been styled just slightly okay I'm gonna zoom in for you guys so you can exit see what's going on and what I want to do now is I want to click the sign up button without actually typing any in because in the URL it should say sign up equal to empty okay so when I click it you guys can see this index.php question mark sign up is equal to empty meaning the right now we should display an error message depending on what is being said in Santa ul okay now another thing we're gonna do at the end is if I were to type something in and something goes wrong if I were to go back to the index page all the data that the typed in is going to be deleted so we want to make sure that they still have the data once they go back to this page here again we're gonna do that at the end here so what we're gonna focus on now is just getting an error message so what I want to do is I want to go inside my code and we have two ways we can do this I'm gonna show you guys one method that we could get the error message and then afterwards I'm going to show you guys the second method okay so we're going to go underneath the form here because I want to display the the aramis is underneath the form so I'm going to open up my PHP tags like so and the first thing I want to do is I want to get the full URL so I can actually see if we have a certain string inside the URL and if we have a string that matches with what we're searching for then we're going to throw some kind of error message so I'm going to say we have a variable called full URL equal to and then we're going to set equal to a string and inside the string we're gonna say HTTP colon forward slash forward slash dollar sign on the score server which is a superglobal that we can use to in order to actually get the URL and then we're going to get the website host so we're going to go inside the brackets and say HTTP underscore post which is the first part of the URL and then afterwards we're going to get the dollar sign underscore server brackets and inside the brackets we're gonna get the - a quest underscore you are I and now we should have the full URL from the browser words actually echo this variable will actually get the entire L okay underneath here I'm going to go ahead and create an if statement because I want to check if we have a certain string in Santa's ul so I'm going to say we have a function called string position or STR POS parentheses and inside here the first parameter is going to be the actual URL so we're gonna get full URL the second one's going to be the string that we want to search for so in this first one I want to search for if any of the fields were left empty and again when we get sent back to the website using using the first error handler you guys can see that it's going to say sign up equal to empty so I'm just going to copy that go back inside my index page and paste it in so what this does is that it says okay if we have this string inside the URL then it's going to tell us the precision of the string inside the UL but we can't really use that for anything so instead we need to check if we even have the string or not so we're going to go after this function here string position and set it equal equal to true so we're checking if this one is inside the UL then do something incentive statement okay inside the if statement here I'm then going to say that we want to echo a string because we just need to create an error message and then we can say something like you did not fill in all fields exclamation mark and what we can also do and again using styling I did actually create two classes that would turn the text green or red so I'm just going to go back inside my code here and create a paragraph tag around my string here just so we have some kind of coloring or something going on so the user can see if they did something wrong or if they actually signed up successfully so I'm just going to say we have a paragraph and a class as equal to single quote single quote and insert a class called error because that's what I created inside my style sheet what's gonna close off the paragraph over here like so and what we do now is basically just copy this paste it on a Neath here create an else--if statement instead and then we check for another string inside the URL so we can go back inside our sign updating PHP take the next one which is character so CH AR go back inside the index page and paste it in side instead of the sign of equal to M T coffee paste we're going to do this two more times so I'm just gonna paste it down twice take the next one which is sign up equal to email which is also an error message because it didn't type in a valid email and I'm just going to change the text under top two here so we're going to say you used invalid characters and just paste it here and just copy it here because it's just about the same we're going to write down here we're just gonna say you used invalid email and invalid email just to type it correctly and the last one down here is going to be the one that checks if we had a successful sign up again the rule of thumb here's that you always need to take for success last inside the script so we're just going to go and do that here so what I'm gonna do is I'm contents want to sign up equal to success which is what I have inside my signup form and I'm going to change the text down here to you have been signed up and then I'm gonna change the class to success because that's what I have inside my style sheet now one last thing we should do here as well is to include a exit function inside each of these if statements afterwards because if the first if statement actually is true and you know we throw some kind of error message depending on what it says in here then we don't need to run the rest of the scripts down here so we just want to exit this script and just not run them at all because it's a waste of time so just gonna include that inside here as well like so now we're going to go back inside the website refresh and as you guys can see it already says you did not fill in all fields because we did already try and submit the form up here as you guys can see if it were to delete and just go back to the front page like normal you guys can see there's no error messages I clicked the button we now have an error message it says you did not fill in all fields if I did fill everything in like so like so like so you guys may notice that the email address is not valid so when I click it says you used an invalid email now for what to actually go up and try and sign everything incorrectly so I'm gonna say Daniel Hilson use mmm toots at gmail.com which is my email and just some random username and password click Sign Up you guys can see it now says you have been signed up and that connects you see I did a spelling mistake here let's actually go and fix that you got signed up okay so this was the first method we can use in order to create error messages inside a website now the second method here might be a little bit better when it comes to making the website a bit user friendly because if I were to create an error in here I'm just gonna fill in everything again signup you guys can see it deletes everything inside the signup form when I get taken back to my error message now if it were to just maybe create an invalid email then it would suck for me as a user if I had a huge form inside the website I need to fill out and then just because I make one mistake in the email address everything gets deleted and I have to fill everything out once more okay we don't want to give the user that bad experience so what we can do here is I'm just going to show you guys one example with the email you guys can just go ahead and repeat the process to do the other error handlers but what we're going to do is I'm going to go back inside my index page and comment out all we did inside the PHP code here now the first thing we're going to do in here is we're going to just repeat everything we did here but using a second method that might be a bit better so I'm going to go down underneath the code here that are just commented out and in this example we don't need to get the entire UL of the website which is kind of nice instead because we did actually say sign up equal to some kind of data which is a get method we can go and use the get method in order to get the error message so the first thing we're going to do is an if statement and I'm going to check if we do actually have signup set inside the UL so if we don't have signup equal to then I'm not just gonna run the code down here okay so I'm going to say inside the condition if we do not by writing exclamation mark heaven is set parentheses set to dollar sign on the score gets brackets called signup which again is the first data inside the statement will create a get method which in this case is signup the second data is after the equal sign which is success okay so inside here I want to say okay if we do not have a sign up inside the URL then I just want to exit the script here because I don't want to do anything because the use apparently haven't submitted the form yet but if the use access of minute deformed then I want to create a our statements curly brackets and then I want to start running all the different error messages so the first thing we're going to do is we're actually going to get the data from the signup data inside the URL so sanam is equal to something and we need to get that piece of data to comes afterwards now we do that by going inside our else statements and setting a variable that we can call something like signup check to check what kind of message we're getting which is equal to dollar sign underscore get brackets single quotes signup which again is the get method from inside the URL and now this variable over here is going to be equal to whatever comes after the equal sign here okay so afterwards we can just simply do another if statement that checks if signup check is equal to a certain string so going to say if it's equal equal to for example MT then it's going to run the code inside the if statement so can again just copy what we have up here at the top the echo and the exit where we echo out some kind of message just gonna paste it in here like so and I'm going to do the same thing below here instead we're just gonna use an if statement of course because we don't want to just keep running if statements tomorr say else if and we're going to change the string that we're checking for inside sign of check two ciao which is character and then we're also going to change the error message inside the actual check here I'm just going to copy the text from up here paste it in copy paste entire thing two more times because we have four different ticks the second one is going to be email and again we just want to copy what we have up here so we're just gonna say you used an invalid email we're just gonna paste it in like so and the last one was this success message like so I'm just gonna copy paste one more time and there we go okay now this should work the exact same way as the previous one we just did up here so which go back and start the website refresh you guys can see it still says use an invalid email because I'm still inside the same page here with their own message if I were to go back and just you know leave something empty it still says you did not fill in all fields so it's still working in the same way as the previous one we just did so what we can do using the get method inside the URL is that we can actually go ahead and if the user makes a mistake inside the form such as not typing the correct email send them back to the form and then take the data and take it back with him to the form and insert them inside the fields that he did not make a mistake in okay so that way we won't have just empty fields when he comes back to the page here so just to show you guys what I mean and inside the signup form we can go to the part of the script that creates the error message for the email and as you guys can see we have signup equal to email inside the UL when we do actually get sent back and we can go out and add more data to it if you want to so I'm going to say and or the ampersand symbol and then I can add more get methods to it if I want to so in this case because the email is incorrect I want to still get the use of the rest of the data that he created inside the form so I'm going to go up here and say okay the email was incorrect so I'm going to leave that one MC when we go back to the the front page but the other ones here might not be be wrong so I want to also send back the first name the last name and the user name I'm going to go ahead and Eve the password MC because I think there should be able to type that once they go back but the other three here I want to send back to the front page so it doesn't have to fill them out again okay so I'm going to pass these inside the UL and say and first is going to be equal to dollar sign first which is our first variable and we're gonna say and last is going to be equal to dollar sign last which is the last variable and then the last one here we're going to include is the username so we're going to say you ID is equal to variable you ID like so so now who were to go back inside the website just to test it out I'm going to just you know try the wrong email address submit and as you guys can see we now have a bunch of data inside the UL up here so what we can do now is I can go inside my index page go to the top where we have the form and I'm going to create an if statement around each input so I'm going to save you have the first one up here which is the first name and create a well first of all the PHP opening and closing tags because we need those and then I'm going to go ahead and create an if statement and I'm going to do the same thing as down here well check if we have a certain get method inside the UL then you should go ahead and get the data from it and put it inside the input okay so I'm just going to copy what we have in here pasted up inside our condition and not take for the signup but instead first which is the get method that has the name as first which is equal to the data we passed on to the form and I'm going to go ahead and go inside my if statement here and then I'm going to just copy the input from down here and echo it out inside here as a string like so and do pay attention to single quotes and double coach because you don't want to to get these confused by by one another and after the placeholder include a value that is going to be equal to two double quotes the get method from inside the URL now we want to create a variable out from this one first so I'm going to set a variable first which is equal to the get method and I'm just going to take this variable here then paste inside the value here like so now the last thing we're going to do here is I'm just going to go after the if statement create a paused statements and say that okay if we did not have this set inside the UL it apparently means that there was no error messages when it came to the first input here so I'm going to just echo out the form that we have down here which is the default form we didn't do anything to so I'm just going to again echo inside the else statements I show you single quotes as my string and I'm just going to leave stay input down here like so so now if we have the get method call first inside the UL then it should go ahead and input it inside our input here if we do not have it then it's just going to leave it empty when we go back to the front page okay now I'm just going to copy the if statement here and the else statement go below here and I'm going to do a second check for the last name so I'm just going to copy the last name input we have down here and delete it insert it inside the two echoes like so and again we're just going to include a value set to variable last so I'm going to go after the placeholder included here you need to change the variable two variable last and again we also need to change the variables up here so they match to get method inside the UL so come see last like so the last thing we need to do to the last name here is also change the the e set statement to last because we want to check if we did actually have a last name inside the UL and now what I want to do is I just want to copy paste this down to the username because that's the last one we did actually want to check for inside the code here so I'm just going to go down create the PHP tags so we can actually include it inside the tags here paste in the if and else statement then just copy the username input down here and paste it inside the echoes and just kind of paste and paste and make sure we change the value inside the one we have inside the if statement and want to change it to UID then we're going to change the variable up here to UID and we also want to change the is set statement to UID like so so now if I were to go back inside the website and just go back to the front page whoops I mean to actually get an error message let's go ahead and check what it says it says undefined index first in line 13 so if we go back here take line 13 we can see that we forgot to aah we need to change it from not set to set of course so we're going to just delete the exclamation marks because we don't want it to to actually create a variable out from the get method if it doesn't exist like so now one more thing we need to change in here which again is a mistake on my end is that we cannot use variables directly inside a string like we did here so we need to actually separate the PHP and the string so we need to say single quotes around the data and then add punctuation in between the single quote and the variables here so we just need to copy what we did here and paste it on top of the rest of them like so and do the same thing here for the first part like so and like so so now we have the variables like to show the inside the inputs here so now if I were to go back inside the website refresh just to reset everything and then type in a proper first name a last name and then pretend we just messed up the email just gonna write something random write a user name such as Denny for 5a and then just a random password if I were to sign up you guys can see we still have the data inside the form once we come back even though we have an error message okay so this is one way you can create error messages and also keep the data inside the form if you were to create an error inside the website I hope you guys enjoyed and I'll see you guys next time you
Info
Channel: Dani Krossing
Views: 144,939
Rating: undefined out of 5
Keywords: How to display error messages using php, create php form error message, create form error message in php, form validation error message in php, php validation error message display, php form validation error message tutorial, php, php tutorial, form error message, form submit error message, html form submit error message, php form submit error message tutorial, php validation, show error messages using php, show form error messages using php, create form error messages using php
Id: HLx-zbl6siM
Channel Id: undefined
Length: 24min 19sec (1459 seconds)
Published: Tue Dec 05 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.