58: How to Create A PHP Contact Form | PHP Tutorial | Learn PHP Programming | HTML Contact Form

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're gonna learn how to create a contact form inside our website and this is something guys a requested of me to do so today we're going to do it as you guys can see here I have a very basic index page that has nothing special inside of it except for maybe a pair of main tanks and inside the main text is what we're going to create the contact form using HTML that it's going to send the email from our website to some kind of email address we have now one thing I want to point out here is that I'm not going to style in this lesson here because it's not really the main purpose of today's tutorial but I did actually create some styling that I'm just going to apply after we created the form just so we have something nice to look at inside the website so if you guys want to style it just go ahead and style it after we created the contact form okay so the first thing I'm going to do inside the main text is I'm going to create a paragraph because I would like to have a title for my contact form so I'm going to say send e-mail just so people know exactly what they're doing inside this form here and underneath here I'm going to create a pair of form tags now I just use to shortcut by clicking tap and inside the attributes a we're just gonna go ahead and add a class to the form tag I'm gonna call this one contact - form and again this is for styling purposes we don't really need this in order to actually get this working technically but I'm just gonna go and include the class now the action we're going to change to contact form that PHP like so and the method is going to be a post method now inside the form tags we're going to create the first input tag we're just going to be set to text we're gonna set the name to name because I would like for people to actually send me the name of you know whoever is sending me an email so I'm going to have that as an input I'm not gonna set a value but I will set a placeholder which I'm going to set to full name then I'm just gonna copy the input paste it underneath here a couple more times and then I'm gonna change the second one to mail because we want to have an email from the person who sent us an email we're going to change the placeholder to your e male now the third input we're going to change the name to subject we're going to change the placeholder to subject now underneath the input we're going to create a text area and we're just going to go ahead and change the name to message we're going to change the placeholder which we don't have yet so I'm just going to copy it from up here and replace the other attributes we had as a default when I created the shortcut and I'm going to change the placeholder to message underneath the text area I'm going to insert a button I'm going to change the type to submits and the name is going to be submit like so inside the button we're going to have a piece of text that says send mail and that is pretty much all we need to have in order to get this form working so what you go inside the browser refresh you guys can see we have a very basic form it's not stylish yet so it doesn't look pretty but if we were to go inside my index page and just add my stylesheet to it so I'm just gonna create a link to my style sheet so I'm gonna say style dot CSS I'm also gonna go ahead and import a new font inside the website because I would like to not just use the default fonts and again this is basic HTML so I'm not really gonna get too much into how to import fonts you guys should know that by now so we're gonna save it refers to browser and as you guys can see we now have a slightly different looking contact form so we can actually type things inside C if you want to and right now nothing is going to happen when we do actually click send mail so what I'm going to do is I'm going to go back inside my website and I'm going to create a new page or a new file I'm going to save this file inside my root folder as contact form dot PHP like so and inside the contact form the PHP file I'm going to open up the PHP tags so you can actually create some PHP code and the first thing I'm going to create is an if statement because we need to actually check if we did actually submit the contact form so I'm going to say is set which is a function then I'm going to check for a post method called submits because that's what I call the button when I clicked it then inside the curly brackets we're going to start creating the actual script that will access send the email so the first thing we need to do is we need to get the actual data that the user wrote inside the inputs and the text area so I'm going to say we have a variable and the first one is going to be named name which is equal to dollar sign underscore post brackets semicolon inside the brackets we're going to say we have an input that had a name as name I'm going to copy this line of code paste it underneath three more times because we had four inputs in total including the text area I'm going to change the second one to subject inside the name over here as well inside the post name and I'm going to change the third one to mail from because you want to know who sex is sending the mail and the way I usually do it is by saying meal from when I name a variable here I'm going to change the post name to male because that was the email the person sent to us and then the last one down is going to be message and I'm going to change it inside the post method as well like so so now we have all the data from the contact form and the next thing we need to do is we need to prepare a PHP function called mail so we're going to say we have a method called male parentheses semicolon and inside the mail function we need to have at least three parameters the first one is going to be the email that we want to send a mail to which is going to be us because it's other people going inside our website sending an email to us the second one is going to be the subject of the mail the third one is going to be the actual message of the mail now we do also have a couple of optional parameters after the actual message and the one that we're going to focus on this episode is the one called a header so what I'm going to do is I'm just going to go back up in between the data we got from the person and the mail function and I'm going to create some of the ones that were missing inside the mail function so right now we need to have a variable that has the email that the mail needs to send to so I'm going to say we have a variable called mail 2 which is equal to some kind of email address and that's going to be my email address which at the moment is Danny ants mmm toots dot net like so now one thing I need to point out here is that if you want to send to your own gmail address it's not going to work because Gmail or Google is going to block the emails that you send using the mail function built into PHP so you will not be able to send to your email address if that's what you wish to do but I do have a way for you to get around it if you want to have the mail sent to your Gmail so I will show you guys how to do that at the end of this episode here ok so after we have the email address we need to have the header that I want to include so I'm going to save a variable called headers which I'm going to set equal to and inside the headers we can actually add extra information to the mail when they do X send it to us this could be who the mail is from so you can actually see you know when you receive the mail who it's actually from it could also be a CC if you want other people to receive the email when the two exes ended there's a couple of things we can add in here but the one we're going to focus on is going to be who the mail is from because that's the one I think is really important so we're going to say double quotes then we're gonna write from with a big F : space now we could just write from my website if you wanted to and then it's actually going to say that it's from your website when you receive the email but I would actually like for it to show the email of the person who sent the message to us so I'm going to go after double quote write dots and then include the the email from the person who actually filled in the contact form which is the mail from up here that we got from the person there we go underneath the headers I'm going to include one more piece of information which is going to be the actual message that we're going to receive inside our email you might be thinking well didn't the user already write a message up here well sometimes we want to have a customized message when we have it sent to us so we're going to do that as well so I'm going to save a variable called txt which stands for text which is equal to double quotes and then I'm going to say that we have a message saying you have received an email from space and then after the double quote I'm going to say dots then the name of the person which is up here pasted in and now what I want to do is I want to end up this line here with the punctuation go down two lines and then actually write the entire message to person sent to us and the way we're going to do this is first of all we need to change back to a string and then we need to add the punctuation at the end of the line and then in order to jump down to lines I'm going to say not forward slash but backslash n which means that we're going to a new line then we're going to add one more because we want to jump down to lines to get a bit of spacing between this message here and the actual message the person sent to me which again if you guys are confused about this I'll show you guys when I do actually receive the email okay afterwards we're going to say punctuation and then I want to add the message so I'm going to say we have a variable called message semicolon and now we have the entire message sent to us so what we need to do now is we just simply need to fill in the mail function that we have down here so we're going to say we have a function called mail - which is up here comma and then we want to have the subject which is going to be up here I'm gonna paste it in comma and then the third one is going to be the text or the message that is being sent to us comma and then I want to include the headers here because I want to have who it's from and there we go now the last thing we need to include is of course a function that takes us back to the front page when this page has loaded and it's done so I'm going to say header parentheses after the mail function and then inside the parentheses I'm going to say we have double quotes location with a big L colon space index dot PHP question mark sent just so we get some kind of confirmation that the male has actually been sent and the contact script has actually been loaded okay so now what we need to do is we need to test it out now this is where you're going to run into a problem because this might not work if you're using localhost as your way to do the PHP lesson here with me again you guys can see i'm also using localhost to actually show you guys so there's two methods we can choose if you want to actually test out if this is actually working or not one being that we need to change the PHP dot ini file inside our localhost server which we downloaded using xampp or we can simply upload this to an online server such as a website to actually test if it's actually working now I chose to not do the complicated thing and change my PHP ini file so I simply uploaded it to my mmm toots net website and just put it inside a different directory just so we could test it out so as you guys can see this is acting my online version that that has all the code working so what I'm going to do before we actually test this out is to show you guys that I have a inbox from inside one that come with which are the people that are actually hosting my website right now and inside my inbox I have no messages that I deleted everything so we could test it out and what I'm going to do is I'm just gonna go ahead and send an email to myself so I'm going to write Danielle Nelson I'm going to say that my email is use mmm toots at gmail.com and again remember this is not the email that you're going to send - this is the email address that I'm saying that my email address is as the person visit your website so this is not going to mess up anything because it's at email address I'm gonna go down to the next line and I'm going to say we have a subject saying hi there or something then I'm going to write a message that says I am testing with this email function inside your website then what we can do is we can actually test out the message and send in and as you guys can see up inside my UL I do get a mail send confirmation if I go inside my webmail you guys can see that if I fresh after a few seconds I've received my email and as you guys can see it says that it's from yous mmm toots which is what I wrote inside my contact form and the subject is hi there if I opened it up you guys can see that we have a from a2 and the missus down here that has spacing in between it because he wrote the backslash n backslash n you know together on a new line so this is basically how we can get this email system working now the last thing we're going to do here is I'm going to show you guys how you can actually receive emails using Gmail if you want to so what I'm going to do here as the solution is that right now I'm inside my hosting service I'm actually logged in to my dashboard over here in a different tab and if you're familiar with your hosting service you should know that if you go inside the dashboard you can go inside the email settings and then you can forward messages that you receive inside your hosting service so the mailbox that I have here which is inside one that comes on website I can actually forward these messages to my email address if I want to so I can either create a new alias down here and forward it so I'm always going to receive the emails in some and email address as well so this is how we'll actually go around fixing this if you want to have messages sent to your email address okay now this was basically one one to show you guys today so hope you guys enjoyed I'll see you guys next time
Info
Channel: Dani Krossing
Views: 475,558
Rating: 4.8922186 out of 5
Keywords: php, php tutorial, php tutorial for beginners full, php tutorial for beginners, php tutorial video, php contact form, php mail form, php e-mail form, php e-mail form tutorial, php contact form tutorial, How to create a php contact form tutorial, how to create a php contact form, html contact form, html contact form tutorial, html mail form, mmtuts, php for beginners, php form, php email form, create php contact form, contact form, contact php, php mail, php mail function
Id: 4q0gYjAVonI
Channel Id: undefined
Length: 14min 43sec (883 seconds)
Published: Mon Nov 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.