How to Request Data from a Contact Form in Laravel and Send to Email(Gmail) Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
greetings from this guy who is here to share his knowledge with you and looking forward to learn from you too so today i'm going to show you how to send data from a contact page in laravel to your preferred gmail and at the end of this tutorial we should have a form like this we should have a form not necessarily exactly as this one but it should have the same functionality whereby a user can provide the name email subject and message and when they click on the submit button we get a flashback message message sent successfully and when we check the email that we will have provided we have that email here it is the subject is bbbbb then we click on the we when we open the email we have the name of the user we have the email of the user and the message so we've grabbed all the data that we had provided the name we've grabbed it the email we've grabbed it the subject you grabbed it and the message you grabbed it so let's get to it so the first thing i'm going to do i'm going to stop the server that you are using to see that form then i'm going to go to a new fresh project that i created here it is untouched now in this new fresh project first thing i'm going to do i'm going to the route routes folder then web.php then i'm going to create a new route i'm going to say route say route then get request because i want to get a form from the server let me terminate this first terminated then i'm going to say when a user goes to slash contact what do i want to happen i want a function to be executed and when that function is executed what do i want to happen i want to return a view return a view by the name contact page you can call this whatever you want contact page let me terminate this one too good so whenever the user goes to the slash contact forward slash contact using get request i want the user to get the contact page view let me create that view i don't have that view so first thing i'll do i'll copy paste this i'll copy this not copy paste ctrl c then i'll go to views resources sorry resources first then views then inside the view i'm going to create a file then i'll paste i like pasting to avoid typos dot blade dot php now inside this contact page and before i continue let me make it clear that the purpose of this tutorial is to teach you how to send data from a contact page form to a gmail so there are some things that i'm not going to explain let's continue now in this contact page i want to have a form so that when i return this page this uh view it should have a form now i have a form here online this is how it looks like i have it's html and it's a css so i'm going to grab there's no there's no javascript so i'm going to grab the html first so i'm going to click here on change view then i'm going to click on html then ctrl a ctrl c to grab everything paste it here and before i paste it let me have a html document here now we have a full html document and in the body that's where i want my form to be there it is let's refresh and see if that is working so we've said when you go to slash contact we execute a function and that function is returning a view by the name contact page and that contact page is here and its content is this form so let's see if that is working site can't be reached oh we've not started the server so we come here we say php artisan server start the server and then the server started let's refresh and see yeah it's working but as you can see hours doesn't look the same as the years and that is because we have not provided the css so let's grab the css click here on css then grab everything controller ctrl c now the styles are usually located in the public folder so come to your public folder create a another folder it's not a must but it's good css enter then in the css let's have our style styling file file then call it whatever you want i'll call it styles dot css enter i'll paste my style then now we have we have classes here like this one how does this form access the classes from these styles you have to provide a link so you come here you see link link then what's the link come here and say asset so the link is uh we have the styles in the public folder css folder then styles.css we don't have to say public folder laravel automatically knows when we are looking for style sheets it knows it will it will try to look for them in the public folder so we will just say css slash styles styles dot css so let's see if that's working go back to the browser refresh good it's working now for us to be able to grab data from this form we need to provide name attributes in that form so let's go back to the form here we will see the name attribute will provide its key as name i'll copy this paste it here then in the email input i want to have the name attribute as email because i'm grabbing email the subject input i want to have the name attribute as subject since i'm grabbing subject then in the text area i can see it's provided so there's no need of writing it again now when a user click on submit button what action do you want to take place we'll come here in the form tag and you will see what action so we want to send data to the server so the action is going to send that at the server to slash contact slash contact i'm using a an external keyboard that is not noisy that's why i'm slow i'm not used to it so please bear with me i don't want to use the laptop's keyboard because i know that the noise is irritating to some people personally when i watch youtube videos and the keyboard is there making noise i don't like it so please bear with me that's why i'm i'm typing slowly then what's the method so since you are sending data to a server we are not receiving no we don't need a comma here since it's an attribute since we are sending a method to a server we are not receiving when we are sending we use the post method so like here we use the get method because we are receiving a form from the server which is this contact page but now we want to send data to a server we use a post method so let's create that route slash contact then with the post method i'll take this one i'll copy this one i'll paste it then the method has changed it's a post the route is the same the function we don't want to return we don't want when somebody clicks on the submit button we return a contact page view that will be very confusing remove this one so what you want to say we want to say request request let's request those keys request terminate this one so we want to request this keys let me show you this name email subject and message from their name attributes so we come here we say since now this is an array because you're requesting several things so we are requesting name the next thing was email the next thing was subject subject and the last thing was message message good now when you request it let's have this in a variable all of this in a variable so i'll come here and say dollar sign let's call it data call it whatever you want then what do i do with this data when you request it for now i want to return it so that you can see if it's working return all the data that you'll collect terminate now let's see that let's see if that's working back to the browser i'll refresh my page i'll have here anything submit please put a nut or there's no arch here submit page expired oh this is because we have not provided the csrf in our form now from just below the form tag let's say at csrf now this this means cross site request for jury and it protects your form from cross site request for jury and the good thing with the laravel is that if you don't provide it your phone will not work you have to provide it so let's see now if it will work of course if you click refresh it will not work because the page had already expired so click back you have to refresh now this another page let's do it try again now this is a new one not try expired let's have subject as anything uh let me put bbb so that you can be able to see the difference cccc submit good we have now the we have been able to grab the data we have the name the key name and the value email value subject value and it's an object so up to this point we have been able to request to request to grab the data from the form now the next thing we're going to do is we want to send this data to our email and we want also to style it so that it has a certain structure we don't want our email to receive something like this looking like this we want our email to receive something structured like this with html so that's what we're going to do next and uh before we do that i want to state that uh you can clearly see that have not validated the inputs because the purpose of this tutorial is not that so make sure you validate in your back end and in your front end it's very important so see in the next video thank you very much
Info
Channel: Alphayo Wakarindi
Views: 1,900
Rating: undefined out of 5
Keywords: laravel, contact form, php, send email
Id: GSwvW3_lXvs
Channel Id: undefined
Length: 15min 44sec (944 seconds)
Published: Thu Jul 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.