add to cart with AJAX and modal

Posted 1 year ago by m.donicova

Hi all I have a simple shop in Laravel where i have for add the product to cart this

<div class="buy">
        <form method="POST" action="{{ URL::to('addcart') }}" class="form-inline" role="form">
            {{ csrf_field() }}
            <input type="hidden" name="product_id" value="{{ $product->id }}">
            <button type="submit"  class="btn btn-primary">Koupit</button>
          </form>
</div>

and for addcart I have

public function postAdd(Request $request) {
        $id = $request->input('product_id');
        $session = $request->session();
        $cartData = ($session->get('cart')) ? $session->get('cart') : array();
        if (array_key_exists($id, $cartData)) {
            $cartData[$id]['qty']++;
        } else {
            $cartData[$id] = array(
                'qty' => 1
            );
        }
        $request->session()->put('cart', $cartData);
        return redirect()->back()->with('message', 'Product Added Successfully!');
    }

now I want for click on submit button show the modal with the information that the product was added to cart a in the same modal to display the actual cart and dispaly two button one for continue in shopping and another for checkout.

I think this must bu some ajax call but in dont know where to start. Is there any example how to solve this? For design I am using the bootstrap and the full code I have on github https://github.com/mardon/MaMushashop

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.