How To Insert Data Using Ajax With Success Message In Laravel

admin_img Posted By Bajarangi soft , Posted On 19-01-2021

We can save data without form tag using ajax and jquery so today we discuss how to do it .

How To Insert Data Using Ajax With Success Message

Step: 1 Create a new table like product and insert some rows of data and make a model.
In this step, we will create first post table and model.
 


public $table = 'product';

    protected $fillable = [
        'product_name', 'product_title',
    ];


Step: 2 Make a view page and create ajax for inserting data as well as success message.
resources/views/welcome.blade.php

 


<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>
        <!-- Styles -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    </head>
    <body>
        <div id="alert" class="alert-success new-window" data-message="my message">
        </div>
        <div class="container">
            <div class="form-group">
                <label class="control-label col-sm-offset-2 col-sm-3" for="company">Product Name:</label>
                <input type="text" name="product_name" id="product_name">
            </div><br>
            <div class="form-group">
                <label class="control-label col-sm-offset-2 col-sm-3" for="company">Product Title:</label>
                <input type="text" name="product_title" id="product_title">
            </div><br>
            <div class="form-group">
                <div class="col-sm-8 col-sm-3">
                    <button type="submit" id="Save" class="btn btn-success save" >submit</button>
                </div>  
            </div>
        </div>
    </body>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
       
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script> -->
    <script type="text/javascript">

    $("#Save").click(function(event) {
        event.preventDefault();
        var product_name = $("#product_name").val();
        var product_title = $("#product_title").val();
        
        $.ajax({
            type: "post",
            url: "{{ url('insert') }}",
            dataType: "json",
            data:{"_token": "{{ csrf_token() }}",product_name:product_name,product_title,product_title} ,
            success: function(data){
                 $('#alert').text('Data Inserted Successfully..');

                setTimeout(function() {
                $('#alert').fadeOut('fast');
                    }, 3000);

            },
            error: function(data){
                 alert("Error")
            }
        });
});
    </script>
    
</html>


Step: 3 Create a function in controller for insert data.
app/Http/Controllers/ProductController.php

 


public function insert(Request $request) 
     {
         $product_name = $request->input('product_name');
         $product_title = $request->input('product_title');

         if($product_name !='' && $product_title !='')
         {

            $data = array('product_name'=>$product_name,"product_title"=>$product_title);
 
        
            $value = Contry::insert($data);
            return response()->json(200);

         
        }
   }

 


Step: 4 Make a route for connection with controller.
app/Http/routes.php

 

Route::post('/insert', 'ContryController@insert');

Related Post