1

I'm trying to add functionality where users can add items to a cart without refreshing the page using Ajax. I succeeded while posting data to the database, but I can't refresh the list automatically (after the data gets posted).

Ajax

jQuery(document).ready(function(){
  jQuery("#addtocartt{{$one->id}}").on('click',function(e){
     e.preventDefault();
     $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
        }
    });
     jQuery.ajax({
        url: "/addtocart",
        method: 'post',
        data: {
           data: jQuery("#name{{$one->id}}").val(),
           image: jQuery("#image{{$one->id}}").val(),
           price: jQuery("#price{{$one->id}}").val(),
           quantity: jQuery("#quantity{{$one->id}}").val(),
        },
        success: function(result){
          var xs = result.qte;
          swal(xs+"Kgs of {{$one->name}}", "Is Added to Cart !!", "success");
        }});
     });
  });

Here's my list that I want to be refreshed.

@foreach($list as $one)
    <text style="font-family: Poppins-ExtraLight;"> {{$one->cart_item}} : {{$one->quantity}} x {{$one->cart_price}} dh = {{$one->quantity*$one->cart_price}}</text> <br>
@endforeach

Controller

function Add(Request $request){
    $item = new cart;
    $item->user_id = Auth::user()->id;
    $item->cart_number = '0';
    $item->cart_item = $request->data;
    $item->quantity = $request->quantity;
    $item->cart_price = $request->price;
    $item->image = $request->image;
    $item->save();
    $list= cart::all()->where('user_id',Auth::user()->id)->where('state','=','delivering');

    return response()->json(['qte' => $item->quantity]);
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Mohammed Chanaa
  • 93
  • 2
  • 10
  • You are going to have to use jquery to update the list. You are currently using php variables which will not be changed after your ajax request. – CUGreen Dec 10 '18 at 22:28
  • @CUGreen can u please tell me how can I do that exactly? – Mohammed Chanaa Dec 10 '18 at 22:30
  • If you are just updating the quantity then you can just change the value of the quantity using jQuery. If it's this stuff `{{$one->cart_item}} : {{$one->quantity}} x {{$one->cart_price}} dh = {{$one->quantity*$one->cart_price}}` I would wrap the values in spans and classes so you can access them easier. Like this `{{$one->quantity}} x – ArtisticPhoenix Dec 10 '18 at 22:40
  • Oh I got It thanks – Mohammed Chanaa Dec 10 '18 at 22:44

1 Answers1

-1

I have not tested this but assuming $list variable returns the cart, you could do something like this:

Your blade template

<div id="some-list-container">
@foreach($list as $one)
    <text style="font-family: Poppins-ExtraLight;"> {{$one->cart_item}} : {{$one->quantity}} x {{$one->cart_price}} dh = {{$one->quantity*$one->cart_price}}</text> <br>
@endforeach
</div>

Your Controller

return response()->json(['list' => $list, 'item' => $item]);

Your Javascript

...
success: function(result) {
  var item = result.item;
  var list = result.list;
  swal(item.quantity + "Kgs of " + item.name, "Is Added to Cart !!", "success");
  // clear the list
  jQuery("#some-list-container").empty()
  // loop through the response list
  for(var i=0; i < list.length; i++) {
    // add to the list
    var add_item = list[i];
    jQuery("#some-list-container").append('<text style="font-family: Poppins-ExtraLight;"> ' 
        + add_item.cart_item + ' : ' 
        + add_item.quantity + ' x ' 
        + add_item.cart_price + ' dh = ' 
        + (add_item.quantity * add_item.cart_price) 
        + '</text> <br>')
  }
}
...

Hopefully that sets you on the right path

CUGreen
  • 3,066
  • 2
  • 13
  • 22