1

I have a function addProduct with message session flash and i would like to display the message success with session flash in laravel using ajax jQuery.

Knowing that the data is saved to the Data Base without displaying the flash message

View:

   <div class="col-md-12">
        @if(Session::has('message'))
         <p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ Session::get('message') }}</p>
      @endif
      @if(Session::has('alert-danger'))
      <p class="alert {{ Session::get('alert-class', 'alert-danger') }}">{{ Session::get('alert-danger') }}</p>
      @endif
      </div>
<script>
 $(document).ready(function() {
    $(document).on('click', "#add", function() {

        var codeProduct = $('#codeProduct').val();

            $.ajax({
              url: "addProduct",
              method: 'POST',
              data: {
                codeProduct: codeProduct
              },
              success: function(data) {
              }
            });
        }
    });
</script>

Controller:

public function addProduct(Request $request){

        $product = new Product();
        $product->codeProduct = $request->codeProduct;
        $product->save();   

        Session::flash('success','Product Suucess!'); 

        return response()->json($product);
    }

2 Answers 2

1

You could create an array to hold the data that you want to return from the JSON request. For example you could have the following variables:

  • Success
  • Message
  • Data

so your code would look like this:

public function addProduct(Request $request){
    $status = false
    $message = "";

    $product = new Product();
    $product->codeProduct = $request->codeProduct;

    if ($product->save()){
        $status = true;
    } else {
        $message = "The product failed to load!";
    }

    return response()->json([
        'status' => $status,
        'message' => $message,
        'data' => $product
    ]);
}

Then from within your AJAX request you can get the data from the response as well as the success status and the message:

var codeProduct = $('#codeProduct').val();

        $.ajax({
          url: "addProduct",
          method: 'POST',
          data: {
            codeProduct: codeProduct
          },
          success: function(data) {

              var status = data.status;
              var message = data.message;
              var product = data.data;

          }
        });

    }
Sign up to request clarification or add additional context in comments.

Comments

1

Just make a success message in the success function like this

I forgot the controller code here it is

if ($product->save()) {
   return response()->json(['data' => $product, 'success' => true, 'message' => 'success'], 200);
} else {
   return response()->json(['success' => false, 'message' => 'error'], 422);
}

first, make a div with the class messages and then select it with jquery

var messages = $('.messages');

Now make a variable called successHtml and make the success message

var successHtml = '<div class="alert alert-success">'+
                '<button type="button" class="close" data-dismiss="alert">&times;</button>'+
                '<strong><i class="glyphicon glyphicon-ok-sign push-5-r"></</strong> '+ data.message +
                '</div>';

Now put the successHtml message into the messages div

$(messages).html(successHtml);

so your code should look like this

<div class="col-md-12">
        @if(Session::has('message'))
         <p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ Session::get('message') }}</p>
      @endif
      @if(Session::has('alert-danger'))
      <p class="alert {{ Session::get('alert-class', 'alert-danger') }}">{{ Session::get('alert-danger') }}</p>
      @endif
       <div class="messages"></div>
      </div>
<script>
 $(document).ready(function() {
    $(document).on('click', "#add", function() {

        var codeProduct = $('#codeProduct').val();

            $.ajax({
              url: "addProduct",
              method: 'POST',
              data: {
                codeProduct: codeProduct
              },
              success: function(data) {
                var messages = $('.messages');

                var successHtml = '<div class="alert alert-success">'+
                '<button type="button" class="close" data-dismiss="alert">&times;</button>'+
                '<strong><i class="glyphicon glyphicon-ok-sign push-5-r"></</strong> '+ data.message +
                '</div>';

                $(messages).html(successHtml);

              }
            });

        }

    });
</script>

1 Comment

any better way to do it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.