Submit Contact Form in PHP Laravel 5 with toastr notifications jquery

Submit Contact Form in PHP Laravel 5 with toastr notifications jquery

Submit Bootstrap Contact Form in PHP Laravel 5 with toastr notifications jquery

In this tutorial, i will tell you how to create simple contact form and on submit apply validation over it in laravel You will get notify with toastr notification on successfully.

As we know, for any organisation contact form comes on first priority because its medium to communicate with site owner.

By using contact form, you can keep records of users who want to connect with you and when you have any updates then easily you can send notification to those users via their email addresses.

Here i will design simple bootstrap contact form having 3 fields : name, email and message.

Step 1: Install Laravel 5.2

Before going with below steps, you should configure your local system first. If Laravel is not installed in your local system then first install it by running following command :

composer create-project --prefer-dist laravel/laravel blog

Ok now, i assume it you have successfully installed laravel project in your path directory.

Step 2: Create ContactEmail Table and Model

Now in this step, create a contact_emails table (having column id,name,email,message,created_at,updated_at) in your database and then create model for contact_emails table.

app/ContactEmail.php
  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class ContactEmail extends Model
  4. {
  5. public $fillable = ['name','email','message'];
  6. }
Step 3: Add Route and Controller

In this step we will create two routes. First route will render form where you fill your details and second route will be used for posting form data with post method.

Add these two routes in your routes.php file.

app/Http/routes.php

Route::get('contact-us',array('as'=>'getcontact','uses'=>'PageController@getContact'));
Route::post('contact-us',array('as'=>'postcontact','uses'=>'PageController@postContact'));

Now we will create a PageController.php in controllers directory.

app/Http/Controllers/PageController.php
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\ContactEmail as ContactEmail;
  5. class PageController extends Controller {
  6. public function getContact()
  7. {
  8. return view('contact');
  9. }
  10. public function postContact(Request $request)
  11. {    
  12.      $this->validate($request, [
  13. 'name' => 'required',
  14. 'email' => 'required|email',
  15. 'message'=>'required'
  16. ]);
  17. ContactEmail::create($request->all());
  18. $notification = array(
  19. 'message' => 'Thanks! We shall get back to you soon.',
  20. 'alert-type' => 'success'
  21. );
  22. return redirect()->back()->with($notification);
  23. }
  24. }

In postContact method, we are validating name,email and message field if any field is empty then it redirect back with errors variable otherwise it create a new record in table and return back with toastr notification.

Step 4: Create contact view page

In this step, we will create a simple bootstrap form for contact us.

resources/views/contact.blade.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>Submit Bootstrap Contact Form in Laravel 5 using toastr notifications jquery</title>
  8. <link rel="stylesheet" href="http://www.expertphp.in/css/bootstrap.css" type="text/css" media="all" />
  9. <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.css">
  10. <script src="http://demo.expertphp.in/js/jquery.js"></script>
  11. <script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="panel panel-primary">
  16. <div class="panel-heading">Submit Contact Form in Laravel 5 using toastr notifications jquery</div>
  17. <div class="panel-body">
  18. {!! Form::open(array('route' => 'postcontact','method'=>'POST')) !!}
  19.              <div class="col-xs-12 col-sm-12 col-md-12">
  20. <div class="form-group">
  21. <strong>Name:</strong>
  22. {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
  23. {!! $errors->first('name', '<p class="alert alert-danger">:message</p>') !!}
  24. </div>
  25. </div>
  26. <div class="col-xs-12 col-sm-12 col-md-12">
  27. <div class="form-group">
  28. <strong>Email:</strong>
  29. {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
  30. {!! $errors->first('email', '<p class="alert alert-danger">:message</p>') !!}
  31. </div>
  32. </div>
  33. <div class="col-xs-12 col-sm-12 col-md-12">
  34. <div class="form-group">
  35. <strong>Message:</strong>
  36. {!! Form::textarea('message', null, array('placeholder' => 'Message','class' => 'form-control','style'=>'height:100px')) !!}
  37. {!! $errors->first('message', '<p class="alert alert-danger">:message</p>') !!}
  38. </div>
  39. </div>
  40. <div class="col-xs-12 col-sm-12 col-md-12 text-center">
  41.                 {!! Form::submit('Save',['class'=>'btn btn-success']) !!}
  42.                 </div>
  43.         {!! Form::close() !!}
  44.     </div>
  45. </div>
  46. </div>
  47. <script>
  48. @if(Session::has('message'))
  49. var type = "{{ Session::get('alert-type', 'info') }}";
  50. switch(type){
  51. case 'info':
  52. toastr.info("{{ Session::get('message') }}");
  53. break;
  54. case 'warning':
  55. toastr.warning("{{ Session::get('message') }}");
  56. break;
  57. case 'success':
  58. toastr.success("{{ Session::get('message') }}");
  59. break;
  60. case 'error':
  61. toastr.error("{{ Session::get('message') }}");
  62. break;
  63. }
  64. @endif
  65. </script>
  66. </body>
  67. </html>

We check in above script if session has any message to display by using Session::has('message') and then apply switch cases over notification what type of alert message should be display via toastr notification.

If you will get any error related "Class form or html not found in Laravel 5" then click here to resolve this error.

Class form or html not found in Laravel 5

Now We have completed a simple contact form and save form data into database. We will tell you more about toastr notification in next tutorial.

We hope this tutorial will help you to create simple contact form for your application.

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort - akyazı escort - arifiye escort - erenler escort - eve gelen escort - ferizli escort - geyve escort - hendek escort - karapürçek escort - karasu escort - kaynarca escort - kocaali escort - otele gelen escort - pamukova escort - sapanca escort - söğütlü escort - taraklı escort
sapanca escort hendek escort karasu escort escort sakarya sakarya escort bayan adapazarı escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort