首页、购物车、添加到购物车如何相互连接?

问题描述 投票:0回答:1

我一直在尝试将网站主页上的“添加到购物车”按钮(用于在线购物)定向到 addtocart 文件和购物车。我似乎找不到问题所在。这是代码。

home.blade.php

<?php
session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>StyleSync Home Page</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <nav class="navbar">
        <div class="container">
            <div class="logo">
                <img src="logo_no_name.png" alt="Logo">
            </div>
            <ul class="nav-links">
                <li><a href="index.php">Home</a></li>
                <li><a href="shoppingcart.php">Shopping Cart</a></li>
                <li><a href="ootdposts.php">OOTD Posts</a></li>
                <li><a href="settings.php">Settings</a></li>
                <li><a href="aboutus.php">About Us</a></li>
                <li><a href="faqs.php">FAQs</a></li>
            </ul>
        </div>
    </nav>

    <div class="container">
        <main>
            <section class="banner">
                <div class="banner-content">
                    <h1>Welcome to Online Shopping!</h1>
                    <p>Discover the latest trends and exclusive deals.</p>
                </div>
            </section>

            <section class="featured-products">
                <h2>Featured Products</h2>
                <div class="product-card">
                    <img src="item1.jpg" alt="Product 1">
                    <h3>Product Name</h3>
                    <p>$99.99</p>
                    <form action="addtocart.php" method="post">
                        <input type="hidden" name="product_id" value="1">
                        <button type="submit">Add to Cart</button>
                    </form>
                </div>
                <!-- More product cards -->
            </section>
        </main>
    </div>

    <div class="footer-container">
        <footer>
            <p>&copy; 2024 Online Shopping. All rights reserved.</p>
        </footer>
    </div>
</body>
</html>

addtocart.blade.php

<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $product_id = $_POST['product_id'];
    
    // Initialize the cart if it doesn't exist
    if (!isset($_SESSION['cart'])) {
        $_SESSION['cart'] = [];
    }

    // Add product to cart
    if (isset($_SESSION['cart'][$product_id])) {
        $_SESSION['cart'][$product_id]++;
    } else {
        $_SESSION['cart'][$product_id] = 1;
    }

    // Redirect to shopping cart page
    header('Location: shoppingcart.blade.php');
    exit;
}
?>

shoppingcart.blade.php

<?php
session_start();
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
?>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Shopping Cart Page</title>
    <link rel="stylesheet" href="StyleNavBar.css">
</head>
<body>
    <nav class="navbar">
        <div class="container">
            <div class="logo">
                <img src="logo_no_name.png" alt="Logo">
            </div>
            <ul class="nav-links">
                <li><a href="{{ route('dashboard') }}">Dashboard</a></li>
                <li><a href="{{ route('home') }}">Home</a></li>
                <li><a href="{{ route('shoppingcart') }}">Shopping Cart</a></li>  
                <li><a href="{{ route('OOTDposts') }}">OOTD Posts</a></li> 
                <li><a href="{{ route('settings') }}">Settings</a></li>
                <li><a href="{{ route('aboutus') }}">About Us</a></li>
                <li><a href="{{ route('FAQs') }}">FAQs</a></li>
            </ul>
        </div>
        </nav>

    <div class="main-container">
        <h1>Shopping Cart</h1>
        <?php if (empty($cart)): ?>
            <p>Your cart is empty.</p>
        <?php else: ?>
            <ul>
                <?php foreach ($cart as $product_id => $quantity): ?>
                    <li>Product ID: <?php echo $product_id; ?> - Quantity: <?php echo $quantity; ?></li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>
</body>
</html>

web.php


Route::get('/home',[HomeController::class, 'home'])->name('home');

Route::get('/shoppingcart', [CartController::class, 'showcart'])->name('shoppingcart');
Route::post('/addtocart', [CartController::class, 'addtocart'])->name('addtocart');

购物车控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CartController extends Controller
{
    public function addtocart(Request $request)
    {
        $product_id = $request->input('product_id');
        
        $cart = session()->get('cart', []);

        if(isset($cart[$product_id])) {
            $cart[$product_id]++;
        } else {
            $cart[$product_id] = 1;
        }

        session(['cart' => $cart]);

        return redirect()->route('shoppingcart');
    }

    public function showcart()
    {
        $cart = session('cart', []);

        return view('shoppingcart', compact('cart'));
    }
}

据说,当用户点击主页上的“添加到购物车”按钮时,它会重定向到购物车页面。

laravel laravel-blade cart shopping-cart
1个回答
0
投票

您的表单没有正确的操作,它应该使用

route()
帮助器:

<form action="{{ route('add-to-cart') }}" method="post">

其余的我觉得还不错。

路线助手文档

© www.soinside.com 2019 - 2024. All rights reserved.