onlinecode

Submit Contact Form in PHP Laravel 5 with toastr notifications jquery

Submit Contact Form in PHP Laravel 5 with toastr notifications jquery

In this post we will give you information about Submit Contact Form in PHP Laravel 5 with toastr notifications jquery. Hear we will give you detail about Submit Contact Form in PHP Laravel 5 with toastr notifications jqueryAnd how to use it also give you demo for it if it is necessary.

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 IlluminateDatabaseEloquentModel;
  3. class ContactEmail extends Model
  4. {
  5. public $fillable=['name','email','message'];
  6. }
namespace App;
use IlluminateDatabaseEloquentModel;
class ContactEmail extends Model
{
    public $fillable = ['name','email','message'];
}

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 AppHttpControllers;
  3. use IlluminateHttpRequest;
  4. use AppContactEmail as ContactEmail;
  5. class PageController extends Controller {
  6. public functiongetContact()
  7. {
  8. returnview('contact');
  9. }
  10. public functionpostContact(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. returnredirect()->back()->with($notification);
  23. }
  24. }
<?php
namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppContactEmail as ContactEmail;

class PageController extends Controller {
    public function getContact()
    {
        return view('contact');
    }

    public function postContact(Request $request)
    {	
    	 $this->validate($request, [
            'name' => 'required',
            'email' => 'required|email',
            'message'=>'required'
        ]);

      ContactEmail::create($request->all());
       $notification = array(
            'message' => 'Thanks! We shall get back to you soon.', 
            'alert-type' => 'success'
        );
       return redirect()->back()->with($notification);
    }
}

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. <!DOCTYPEhtml>
  2. <htmllang="en">
  3. <head>
  4. <metacharset="utf-8">
  5. <metahttp-equiv="X-UA-Compatible"content="IE=edge">
  6. <metaname="viewport"content="width=device-width, initial-scale=1">
  7. <title>Submit Bootstrap Contact Form in Laravel 5 using toastr notifications jquery</title>
  8. <linkrel="stylesheet"href="http://www.onlinecode.org/css/bootstrap.css"type="text/css"media="all"/>
  9. <linkrel="stylesheet"type="text/css"href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.css">
  10. <scriptsrc="//js/jquery.js"></script>
  11. <scriptsrc="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
  12. </head>
  13. <body>
  14. <divclass="container">
  15. <divclass="panel panel-primary">
  16. <divclass="panel-heading">Submit Contact Form in Laravel 5 using toastr notifications jquery</div>
  17. <divclass="panel-body">
  18. {!! Form::open(array('route' => 'postcontact','method'=>'POST')) !!}
  19.              <divclass="col-xs-12 col-sm-12 col-md-12">
  20. <divclass="form-group">
  21. <strong>Name:</strong>
  22. {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
  23. {!! $errors->first('name', '<pclass="alert alert-danger">:message</p>') !!}
  24. </div>
  25. </div>
  26. <divclass="col-xs-12 col-sm-12 col-md-12">
  27. <divclass="form-group">
  28. <strong>Email:</strong>
  29. {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
  30. {!! $errors->first('email', '<pclass="alert alert-danger">:message</p>') !!}
  31. </div>
  32. </div>
  33. <divclass="col-xs-12 col-sm-12 col-md-12">
  34. <divclass="form-group">
  35. <strong>Message:</strong>
  36. {!! Form::textarea('message', null, array('placeholder' => 'Message','class' => 'form-control','style'=>'height:100px')) !!}
  37. {!! $errors->first('message', '<pclass="alert alert-danger">:message</p>') !!}
  38. </div>
  39. </div>
  40. <divclass="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>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Submit Bootstrap Contact Form in Laravel 5 using toastr notifications jquery</title>
   
    <link rel="stylesheet" href="http://www.onlinecode.org/css/bootstrap.css" type="text/css" media="all" />
    <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.css">

    <script src="//js/jquery.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
</head>
<body>
<div >
<div >
 <div >Submit Contact Form in Laravel 5 using toastr notifications jquery</div>
  <div > 

       {!! Form::open(array('route' => 'postcontact','method'=>'POST')) !!}
			   <div >
                    <div >
                        <strong>Name:</strong>
                        {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
                        {!! $errors->first('name', '<p >:message</p>') !!}
                    </div>
                </div>
                 <div >
                    <div >
                        <strong>Email:</strong>
                        {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
                        {!! $errors->first('email', '<p >:message</p>') !!}
                    </div>
                </div>
                <div >
                    <div >
                        <strong>Message:</strong>
                        {!! Form::textarea('message', null, array('placeholder' => 'Message','class' => 'form-control','style'=>'height:100px')) !!}
                        {!! $errors->first('message', '<p >:message</p>') !!}
                    </div>
                </div>
                <div >
				{!! Form::submit('Save',['class'=>'btn btn-success']) !!}
				</div>
		{!! Form::close() !!}
	</div>
</div>
</div>
<script>
  @if(Session::has('message'))
    var type = "{{ Session::get('alert-type', 'info') }}";
    switch(type){
        case 'info':
            toastr.info("{{ Session::get('message') }}");
            break;
        
        case 'warning':
            toastr.warning("{{ Session::get('message') }}");
            break;

        case 'success':
            toastr.success("{{ Session::get('message') }}");
            break;

        case 'error':
            toastr.error("{{ Session::get('message') }}");
            break;
    }
  @endif
</script>
</body>
</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.

Show Demo

Label :

PHP

MySQL

Laravel PHP Framework

jQuery

How To

MVC

CSS

toastr js

JavaScript

Database

Hope this code and post will helped you for implement Submit Contact Form in PHP Laravel 5 with toastr notifications jquery. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version