数据未使用Ajax传递到其他页面

时间:2017-12-06 06:42:28

标签: php ajax laravel laravel-5

我一直试图在购物车系统中使用ajax,价值很容易被添加到主页的购物车中,但是当打开cart.blade.php页面时,产品不会显示。我认为数据正在cart.blade.php页面中传递。

index.blade.php

<div class="widget widget-control-header widget-shop-cart js-widget-shop-cart">
    <a class="control" href="{{route('cart.index')}}">
        <p class="counter">0</p>
        <span class="lnr lnr-cart" id="checkout">
        </span>

        <div class="wrapper-control-item item-add-cart js-action-add-cart">
            <a class="animate-icon-cart" href="#">
                <span class="lnr lnr-cart" >
                </span>
            </a>
        </div>
    </div>
</div>

这是包含所有类的ajax代码。 &#39; JS-动作加推车&#39;用于调用购物车ajax。

添加到cart.js

$(function() {
    var actionAddToCart = $('.js-action-add-cart');
    var cart            = $('.js-widget-shop-cart');
    var itemWishList    = $('.js-notify-add-wish-list');
    var items;

    var cartItems ={items:[]};

    actionAddToCart.each(function() {
        $(this).on('click', function(e) {
            var productid    = $(this).closest('.product').find('a').attr('id');
            var productName  = $(this).closest('.product').find('.product-name').text();
            var productPic   = $(this).closest('.product').find('img').attr('src');
            var productPrice = $(this).closest('.product').find('.price').text();

            if($(this).hasClass( "is-added" )){
                alreadyAddedToCart(productName);
            }else{
                $('#cartHover').append("<li style='width: 200px;border-bottom: 1px solid black;padding: 10px;padding-top:0px;position: relative'>\
                    <span style='width: 50px; position: relative'>"+productName+"</span>\
                    <span style='width: 30px; margin-left: 30px; position:relative'>PKR: "+productPrice+"</span>\
                    <i class='cancel glyphicon glyphicon-remove-circle' style='right:5px; top:5px; position: absolute;float:right'></i>\
                    </li>");

                cartItems.items.push({id: productid, name: productName, pic: productPic , price :productPrice});
                items = JSON.stringify(cartItems);

                e.preventDefault();
                notifyAddToCart(productName);
                $(this).addClass('is-added');
                setTimeout(function(){
                    updateCart();
                }, 1000);
            }

        });
    });

    $('#checkout').click(function(){
        window.location.href = 'cartSession.blade.php?products='+items;
    });

    function updateCart() {
        //show counter if this is the first item added to the cart
        ( !cart.hasClass('items-added') ) && cart.addClass('items-added');
        var cartItems = cart.find('.counter'),
        text = parseInt(cartItems.text()) + 1;
        cartItems.text(text);
    };
});

cartSessions.blade.php

<?php
session_start();
$_SESSION['products'] =$_GET['products'];
header('Location: shop-cart.blade.php');
exit();

1 个答案:

答案 0 :(得分:0)

当Laravel通过中间件处理启动会话时,您真的不需要在刀片模板中使用session_start()。您还希望通过路径提供刀片模板,而不是直接访问它们。创建一条路径,该路由具有自己的闭包或指向控制器方法。

路由它自己的关闭

// ------------------------------------------
// routes.php
// ------------------------------------------

use Illuminate\Http\Request;

Route::get('/cart', function(Request $request)
{
    // Get the products from the request
    $products = $request->products;

    return view('shop-cart', ['products' => $products]);
});

指向控制器的路线

// ------------------------------------------
// app/Http/routes.php
// ------------------------------------------

Route::get('/cart', [
    'as'   => 'cart', 
    'uses' => 'CartController@show'
]);


// ------------------------------------------
// app/Http/Controllers/CartController.php
// ------------------------------------------

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CartController extends Controller {

    /**
     * Show the cart.
     *
     * @return Response
     */
    public function show(Request $request)
    {
        // Get the products from the request
        $products = $request->products;

        return view('shop-cart', ['products' => $products]);
    }
}

在这里,我们只需从请求中获取产品,并将其作为名为$products的变量传递给视图。您需要对javascript进行以下更改:

$('#checkout').click(function(){
    window.location.href = '/cart?products='+items;
});