Creating a dynamic checkout page with removable items for an e-commerce site

Hey everyone! I’m working on a checkout page for my online store and I’m stuck. I want customers to be able to remove items from their cart before they print the bill. I’ve set up buttons for each item, but I can’t get them to work properly.

Here’s what I’ve tried:

<?php
if (isset($_SESSION['customer'])) {
    $cartId = $_SESSION['basket'];
    $query = "SELECT * FROM `$cartId`;";
    if ($data = mysqli_query($connection, $query)) {
        if (mysqli_num_rows($data) > 0) {
            echo '<table>';
            echo '<tr><th>Product</th><th>Amount</th><th>Price</th></tr>';
            while ($item = mysqli_fetch_array($data)) {
                $itemName = $item['product'];
                echo '<tr>';
                echo '<td>' . $item['product'] . '</td>';
                echo '<td>' . $item['amount'] . '</td>';
                echo '<td>' . $item['cost'] . '</td>';
                echo '<td><form method="GET"><button name="' . $itemName . '">Delete</button></form></td>';
                echo '</tr>';
            }
            if (isset($_GET[$itemName])) {
                echo $itemName;
            }
        }
    }
}
?>

The buttons show up, but clicking them doesn’t do anything. I’ve tried both GET and POST methods, and I can see the item names in the URL, but my SQL query isn’t working. I even tried using echo to check if the button names are correct, but nothing shows up.

Any ideas on how to fix this? I’m really hoping to get those remove buttons working so customers can adjust their order before printing. Thanks in advance for any help!

Hey there ClimbingMountain! Your checkout page sounds like a cool project. Have you thought about using AJAX for those remove buttons? It could make things smoother for your customers.

I’m curious - what made you choose PHP for this? Are you using any frameworks or just vanilla PHP?

One thing that might help is to use unique IDs for each item instead of the product name. Maybe something like:

echo '<td><button class=\"remove-item\" data-item-id=\"' . $item['id'] . '\">Remove</button></td>';

Then you could use JavaScript to handle the click and send an AJAX request to a PHP script that actually removes the item from the database.

What do you think about that approach? Have you tried anything like it before?

Also, I’m wondering how you’re handling the total price calculation when items are removed. That could be an interesting challenge too!

Let me know if you want to brainstorm more ideas. I’m always excited to chat about e-commerce solutions!

hey dude, i think i got a solution for ya. instead of using GET, try using POST with a form for each item. somethin like this:

echo '<td><form method="POST">
<input type="hidden" name="item_id" value="' . $item['id'] . '">
<button type="submit" name="remove">Delete</button>
</form></td>';

then in ur PHP, check for the POST action and remove the item. this shud work better than ur current setup. lmk if u need more help!

I’ve dealt with similar issues before, and here’s what worked for me:

Instead of using GET or buttons named after products, try using hidden input fields with unique IDs for each item. Then use JavaScript to handle the remove action.

Here’s a quick example:

echo '<td><form method="POST">\n<input type="hidden" name="item_id" value="' . $item['id'] . '">\n<button type="submit" name="remove">Delete</button>\n</form></td>';

Then in your PHP, check for the POST action:

if (isset($_POST['remove']) && isset($_POST['item_id'])) {
    $item_id = $_POST['item_id'];
    $delete_query = "DELETE FROM `$cartId` WHERE id = '$item_id'";
    mysqli_query($connection, $delete_query);
    // Redirect to refresh the page
    header("Location: " . $_SERVER['PHP_SELF']);
    exit();
}

This approach should solve your issue while keeping the code relatively simple. Hope this helps!