how to insert data using ajax in laravel with datatables?

Today, We want to share with you how to insert data using ajax in laravel with datatables?.In this post we will show you Insert Update and Delete record with AJAX in Laravel 6, hear for Laravel & Ajax – Insert data into table without refreshing we will give you demo and example for implement.In this post, we will learn about how to insert data using ajax in laravel with datatables with an example.

how to insert data using ajax in laravel with datatables?

There are the Following The simple About how to save data in database using ajax in laravel Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 6 Inserting data to the database with Ajax , so the How to insert data using Laravel, Ajax is used for this example is following below.

Step 1: Define a Laravel 6 Route

web.php

Route::resource('/memberInformation','MemberInfoController');

Step 2: Create a Controllers in Laravel

MemberInfoController.php (controller)

namespace App\Http\Controllers;

use App\MemberInfo;
use Illuminate\Http\Request;

class MemberInfoController extends Controller
{
public function create()
    {
        return view('memberInformation.create');
    }

public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required',
        ]);
        MemberInfo::create($request->all());
        return json_encode(array(
            "statusCode"=>200
        ));
    }
}
 

Step 3: Create a Laravel Model

MemberInfo.php (Model)

namespace App;

use Illuminate\Database\Eloquent\Model;
class MemberInfo extends Model
{
    protected $table = 'members_details';
    protected $fillable = [
        'type','name', 'email','mobile','address'
    ];
}

Step 4: Create a Laravel Blade File

create.blade.php

@extends('memberInformation.layout')
  
@section('content')
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Add New Member</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-primary" href="{{ route('memberInformation.index') }}"> Back</a>
        </div>
    </div>
</div>
   
@if ($errors->any())
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<div class="form-group cart-jdk">
        <input type="hidden" name="_token" id="csrf" value="{{Session::token()}}">
    <label for="email">Member First Name:</label>
    <input type="text" class="form-control custom-dsp" id="name" placeholder="Enter Member First Name" name="name">
  </div>
  <div class="form-group cart-jdk">
    <label for="email">Email:</label>
    <input type="email" class="form-control custom-dsp" id="email" placeholder="Enter Email" name="email">
  </div>
  <div class="form-group cart-jdk">
    <label for="email">Mobile:</label>
    <input type="text" class="form-control custom-dsp" id="mobile" placeholder="Enter Mobile" name="mobile">
  </div>
  <div class="form-group cart-jdk">
    <label for="email">Address:</label>
    <input type="text" class="form-control custom-dsp" id="address" placeholder="Enter Address" name="address">
  </div>
  <button type="submit" class="btn btn-primary" id="storememberbtn">Submit</button>
</div>
<script>
$(document).ready(function() {
   
    $('#storememberbtn').on('click', function() {
      var name = $('#name').val();
      var email = $('#email').val();
      var mobile = $('#mobile').val();
      var address = $('#address').val();
      var password = $('#password').val();
      if(name!="" && email!="" && mobile!="" && address!=""){
        //   $("#storememberbtn").attr("disabled", "disabled");
          $.ajax({
              url: "/memberInformation",
              type: "POST",
              data: {
                  _token: $("#csrf").val(),
                  type: 1,
                  name: name,
                  email: email,
                  mobile: mobile,
                  address: address
              },
              cache: false,
              success: function(responseOutput){
                  console.log(responseOutput);
                  var responseOutput = JSON.parse(responseOutput);
                  if(responseOutput.statusCode==200){
                    window.location = "/memberInformation";				
                  }
                  else if(responseOutput.statusCode==201){
                     alert("Error occured !");
                  }
                  
              }
          });
      }
      else{
          alert('Please fill all the field !');
      }
  });
});
</script>
@endsection

Step 5: Main Master Layout Blade file

layout.blade.php

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <title>PHP Laravel 6 CRUD using jQuery Ajax</title>
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
  
<div class="container">
    @yield('content')
</div>
   
</body>
</html>
 

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about add record using ajax laravel 6.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   How to Open URL in New Tab using ReactJS
  2.   CodeIgniter JQuery Ajax Request Example
  3.   AngularJS Ajax Live search
  4.   Laravel 6 JQuery Ajax Post Request Example
  5.   Select / Deselect all checkboxes using jQuery
  6.   onclick update database in laravel
  7.   jQuery AJAX laravel 6 Delete Data from database
  8.   jQuery Ajax POST example with Laravel 7/6
  9.   jQuery Ajax Delete Request with Laravel
  10.   Laravel 6 Update Increment column