PHP Front To Back [Part 15] - PHP Contact Form

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys local to your next PHP front-to-back video in this video we're going to be creating a simple contact form and we're going to use most of the stuff that we've learned up to this point as well as the PHP mail function and a few other things okay we're also going to add a little bit of server-side validation and setup error messages so I'm going to create in my PHP sandbox and create a new folder and we're going to call this website three and then we're going to create a new file and save it and index dot PHP alright so this is the one file we're going to create we're going to put all of our HTML here our form and also going to process the PHP in this file alright so let's go ahead and add some basic HTML tags and for the title we'll just a contact us alright now I do want to use bootstrap just to make things look a little better so I'm going to use a goose watch which is basically just customized bootstrap templates so let's grab this cosmo i'm just going to grab the link up here okay no just going to say link rel stylesheet' href and then paste that it and you guys probably see me use this boots to watch a lot and the reason I use it is because it's really easy just to include the link and it makes it look a little better than just standard bootstrap this is a for production application I would install bootstrap using flower or something like that and include it locally alright so now that we have that set up let's go ahead and add the markup first we have the navbar and I'm just going to paste that in just a very simple navbar we'll save that and let's go and visit local host PHP sandbox slash web site 3 okay so there's our navbar now I'm going to paste in the form but I'm not going to put any of the dynamic PHP in there yet so let's paste that in alright and just to go over this it's a simple HTML form method it's post I haven't added an action yet and we have a couple dibs with a label and an input and I notice we do have a name attribute for each one and the last one is the message which is a text area and then we have a simple button and notice like with the name on the submit as well because that's what we're going to check for up in the PHP we're going to check to see if it submit is there okay now for the action we could just put index.php but i'm actually going to use the server superglobal they make sure you echo this out or we're going to use PHP underscore self and that's going to submit to this file all right and then up here above all the HTML we're going to put our PHP tags alright and just to test this out what we're going to do is check for the submit and we're going to use the filter has bar function that we learned about in I think it was the last video so we'll set up an F here and I'm going to say if filter has bar and we're submitting a post form so we want to put in here input underscore post and then we're checking for the submit alright and if that is true then we're just going to echo out submit all right so let's go ahead and save that I'll reload there's our form and if we try to submit up here we get submitted ok because it's looking at this right here the name equals submit ok so let's get rid of that and next thing we want to do after we submit is get two form data and we're going to put those into variables so let's set email just that to toast email okay actually let's put the name first so name equals post name and then we also have the message alright so now we have our post variables in regular variables and we want to do a little bit of validation so all the fields are going to be required so let's do that well today check required fields so it's going to do an if statement here and if else and all stuff that we've learned already and in here we're going to check to see if each one is not empty okay and there's a function called empty that we can use to see if there's actually something in there so we're going to say it's not empty email and not empty name and not empty message okay so if none of these are empty then we pass okay if not then we failed now if it does fail I want it to show out a message so up here I'm going to create two variables so it's going message bars alright so we're going to set one called msg and we're going to set that to nothing for now and one called msg class and set that to nothing okay the reason I'm doing the class is because with bootstrap and most front-end frameworks you have you can have alerts and you can set the class to different colors or different statuses all right so we want the errors to be read in the success to be green so down here where we failed all we're going to do is put stuff in those variables so we'll put the message we'll say please fill in all fields and then for the class msg quack in bootstrap cakes alert - danger all right that'll make it read so down here in the container let's go ahead and test for message so we'll say PHP if the if message I'm going to use the shorthand syntax actually we're going to say if message is not equal to nothing because it starts out as an empty string and if that's true then we want to put out the alert so div class with bootstrap we have to do alert and then the alert danger or alert success whatever it's going to be so here we're going to PHP echo msg class alright this is going to be which is going to be the alert so in here we want to output the message echo msg okay so that should actually work if it passes for now with just echo out past okay let's just reset this completely undefined variable message oh that should be msg you guys probably talked about okay so if we submit please fill in all fields if we put in two fields we're still going to get that if we put in all of them then we passed alright so that's all set as far as required fields now if they passed that they're going to have to test the email okay the email has to be valid and we can use filter bar which we talked about in the last lesson all right so let's say check email and we're going to use if and it's a filter underscore bar okay and we want to pass in here the email which is in that variable and then we want to add filter validate email okay and we want to set we want to say if this is equal to false okay then it's going to fail all right now we'll put an else and this will be passed all right you don't have to write these comments I'm just using them too so just to try to explain what exactly is going on all right but if it fails we're going to again set a message and a class so right key and put that in all right and then this will be for now we'll just echo low-caste all right so let's go ahead and reload this and again our required fields now if we fill everything in but the email isn't valid what we need to change the message please use a valid email all right so if we reload then it says please use a valid email okay if we put in everything and we do use a valid email then it passes all right so our validation and messaging is all set well one thing I want to change is that when we enter this stuff into and we fail it's gone whatever we put in the fields all goes away we don't want that because if we have a lot of fields here and they fail on just one of them we want the other stuff to stay so that's where the value attribute comes in down here okay right now they're set to none but what we're going to do is test to see if there's a post value and if there is we're going to keep it output if there isn't then it'll stay clear okay so in this value right here we're going to say PHP I'm going to use the shorthand the ternary operator which we learned about I think - two videos ago and I'll say echo is that okay so we want to see if post on the SCORM post and this is the name field so we wanted a post name is that then we want to output name and if it's not then we want to output nothing all right and then we'll end the PHP so let's copy that and we're going to put that in the value here as well and just change this to email and change this to email for the message that's going to go within the text area so we'll paste that in and I'll change these to message change that to message all right and I'm actually going to use HTML special chars up here HTML special chars and we'll wrap these post values all right so let's save that and we'll try it out so let's just say test one the email is going to fail test three and let's submit and we failed but we still have our data in the form all right so that's that now we can move on to actually setting up to send the email so we're going to use the PHP mail function but we need to set a couple values a couple variables before that so we need to setup the recipient email so the email address where you want this stuff to go so let's say to email and you want to use your own hosting or your own server so I'm going to say support at strawberry Mediacom alright and then we're going to set up the subject which I guess we don't need these comments it's pretty obvious what it what they are so it's the subject and the subject we're going to say contact request from and then we can actually put we'll concatenate their email address or I'm sorry their name like that alright then we need to create the body okay so the body of the email and you can have HTML in here so let's do an h2 I'll just say contact request let's see let's go on a new line here we'll put in H for say name and then under that and put a paragraph and that's going to be the name variable alright and I'll just copy this so we have name email and the message and you can use whatever kind of layout you want okay and then we're going to set the headers so let's say headers and the first thing we're going to set is the line version so let's say mime - conversion and that's going to be 1.0 all right and then we're going to have to do a carriage return and a new line so to do that let's see we're going to say / r / n all right and then we're going to add on to this headers variable so let's say headers and you want to make sure you do dot equals because that's a pen okay it's not going to overwrite this it's going to add on to it so we also want to specify the content type and you can set any header value you want here but we're going to select contact type to text HTML and also going to set the character set which are set I'm gonna set that let's set that to UTF - 8 all right and then we're also going to concatenate a carriage return and a newline and that should be good now we want the from we need to put who loses from in the cutter so let's just say additional headers and I'm going to take that variable and add on to it and we want to add from C say from and then we're going to concatenate here named use this syntax put the female like that and then / r / end all right so those are the headers now we can use the mail function and we're going to put that in an if statement so that this will say if it sends or if it doesn't so let's say male and this is going to take in the recipient email it's going to take in the subject it's going to take the body and it's going to take the headers okay those are the parameters that this function needs so if everything goes okay then we're going to set a message let's just go here we'll say email sent and it'll change this to your email has been sent and we're going to change this to success which will make it green and then if it fails let's say your email was not sent and we'll keep that a danger all right and that should be it it should now send so let's save that now if you're using xampp like I am this isn't going to work let's just see what happens so we'll just say John Doe email text we'll just say hello a message and submit and we're going to get this error this is failed to connect to mail server at localhost that's because i'm using xampp and we don't have a mail server setup so what I'm going to do is upload this to travesty mediacom slash test all right so let's grab it and see htdocs and rally PHP sandbox website 3 I'm going to bring that index file up here ok and then we're going to go to travesty media.com slash test and there's our form and then we'll fill this out again we'll say hello I need help and we'll submit and we get your email has been sent so now I'm going to go to my gmail account and I'm going to pull that down and there it is from John Doe contact requests from John Doe contact requests we have the name the email and the message alright now that may go to your spam folder especially if you're using Gmail or Yahoo or something like that it's kind of hard to really figure out why it's going to spam if you go if you if you use better SMTP settings you may have a better chance of it not going to spam but that's a whole other subject altogether but I just wanted to show you how to create a very simple contact form using the PHP mail function alright and as well as some validation so hopefully you guys enjoyed the we're going to keep moving along in this series now it might be a couple days between episodes because I'm really busy right now but I do want to definitely get into databases in MySQL and all that so stay tuned and I will see you in the next episode
Info
Channel: Traversy Media
Views: 115,771
Rating: undefined out of 5
Keywords: php contact form, php, contact form, php form, php form validation, form validation
Id: tJ5eUgOxITE
Channel Id: undefined
Length: 19min 56sec (1196 seconds)
Published: Thu Mar 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.