K
K
kos94ok2018-06-29 14:30:29
Laravel
kos94ok, 2018-06-29 14:30:29

Sending a GET request via AJAX gives me a 500 error. What can be wrong?

5b36198ca1b31126458452.pngI'm trying to send an AJAX GET request:

$('document').ready(function(){
    $('#plus').click(function(){
             $.ajax({
             method: 'GET',
             url: '/cart?product_id=$item->id&decrease=1',
             success: function(){
               alert('Успешно');
             },
             });
        });
      $('#minus').click(function(){
             $.ajax({
             method: 'GET',
             url: '/cart?product_id=$item->id&decrease=1',
             success: function(){
               alert('Успешно');
             },
             });
        });     
    });

Throws 500 error.
I figured it was the links. ( /cart?product_id=$item->id&decrease=1, /cart?product_id=$item->id&decrease=1). Namely, in $item->id.
Is there any way to fix this problem?

Controller
public function cart(){
    	$seo_title = 'Корзина';
    	$seo_discription = 'Корзина';
    	$seo_keywords = 'Корзина';
   if (Request::isMethod('post')) {
        $product_id = Request::get('product_id');
        $size = Request::get('size');
        $product = products::find($product_id);
        $size = Db::table('sizes')->where('id', $size)->first();
        Cart::add(array('id' => $product_id, 'name' => $product->title, 'qty' => 1, 'price' => $product->price, 'options'=>['image' => $product->image, 'sizes'=>$size->id ]));
    }

    //increment the quantity
    if (Request::get('product_id') && (Request::get('increment')) == 1) {
     $item = Cart::search(function($key, $value) { return $key->id == Request::get('product_id'); })->first();
  Cart::update($item->rowId, $item->qty + 1);
  }
    //decrease the quantity
    if (Request::get('product_id') && (Request::get('decrease')) == 1) {
  $item = Cart::search(function($key, $value) { return $key->id == Request::get('product_id'); })->first();
  Cart::update($item->rowId, $item->qty - 1);
  if ($item->qty == 0) {
    return redirect()->route('cart');
  }
  }
  if ((Request::get('product_id')) && (Request::get('remove')) == 1){
  Cart::destroy();
  return redirect()->route('cart');
  }
    $cart = Cart::content();
    return view('cart', array('cart' => $cart, 'title' => 'Welcome', 'description' => '', 'page' => 'home','seo_title'=>$seo_title,'seo_keywords'=>$seo_keywords,'seo_discription'=>$seo_discription));
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RidgeA, 2018-06-29
@RidgeA

Well, apparently id === "$item->id" is wrong.
Can you put the correct value there?

Y
Yan-s, 2018-06-29
@Yan-s

This is all because in JS you are trying to execute the PHP $item->id construct, but in reality you are just inserting it as a string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question