PHP购物车问题:Firefox无法删商品+结账邮件发送失败求助
Hey there, let's break down and fix these two issues you're facing with your shopping cart site!
Looking at your cart.php code, there are two key problems causing this behavior:
1. Incorrect index value for remove links
You initialize $index = 0 outside your product loop, and only increment it after the loop finishes. This means every remove link uses the same index=0 value—so no matter which item you click to remove, it only deletes the first item in the cart. Firefox handles this inconsistency more strictly than other browsers, making the bug obvious here.
Fix this by moving the $index++ inside the loop:
<?php $cart = unserialize(serialize($_SESSION['cart'])); $s = 0; $index = 0; for($i=0; $i<count($cart); $i++){ $s += $cart[$i]->pPrice * $cart[$i]->qty; ?> <tr> <td> <div class="shopcart"> <!-- Now $index correctly matches the current product's position --> <button style="width:150px; height:50px;"><a href="cart.php?index=<?php echo $index;?>" onClick="return confirm('Please confirm deletion of the chosen product.')">Remove</a></button> </div> </td> <!-- Rest of your table cells --> </tr> <?php $index++; // Move this line inside the loop to increment after each product } ?>
2. Invalid HTML structure (button nested inside anchor tag)
HTML doesn't recommend nesting <a> tags inside <button> elements—different browsers handle this event conflict differently, and Firefox often blocks the anchor's navigation behavior as a result.
Replace the button+anchor combo with one of these cleaner options:
<!-- Option 1: Style anchor tag to look like a button --> <a href="cart.php?index=<?php echo $index;?>" onClick="return confirm('Please confirm deletion of the chosen product.')" style="display:inline-block; width:150px; height:50px; background:#ccc; text-align:center; line-height:50px; text-decoration:none; color:#000;"> Remove </a> <!-- Option 2: Use button's onclick event to trigger navigation --> <button style="width:150px; height:50px;" onClick="if(confirm('Please confirm deletion of the chosen product.')){window.location='cart.php?index=<?php echo $index;?>';}"> Remove </button>
Bonus optimization: Simplify cart manipulation
You don't need to serialize/unserialize $_SESSION['cart'] to modify it—this adds unnecessary overhead. Simplify the remove logic:
if(isset($_GET['index'])) { $index = (int)$_GET['index']; // Sanitize input to prevent injection if(isset($_SESSION['cart'][$index])){ unset($_SESSION['cart'][$index]); $_SESSION['cart'] = array_values($_SESSION['cart']); // Re-index the array } }
Your current checkout.php has critical issues with the mail sending logic:
1. Incorrect mail() function parameters
PHP's mail() function requires mail($to, $subject, $message, $headers)—you're passing the sender email as the message, so the email is empty and missing all order details.
2. No use of cart session data
You aren't pulling the cart items from $_SESSION['cart'] to include in the email, so even if it sent, you'd get no order information.
3. Missing essential email headers
Without setting From or Content-Type headers, your email is likely being flagged as spam or rejected by email providers.
Here's the fixed checkout.php code:
<?php session_start(); // Redirect empty carts back to products page if(!isset($_SESSION['cart']) || empty($_SESSION['cart'])){ echo "<script> alert('Your cart is empty!'); window.location='products.php'; </script>"; exit; } // Build order content from cart session $orderContent = "New Purchase Order:\n\n"; $total = 0; foreach($_SESSION['cart'] as $item){ $subtotal = $item->pPrice * $item->qty; $total += $subtotal; $orderContent .= "Product ID: {$item->pID}\n"; $orderContent .= "Product Name: {$item->pName}\n"; $orderContent .= "Price: €{$item->pPrice}\n"; $orderContent .= "Quantity: {$item->qty}\n"; $orderContent .= "Subtotal: €{$subtotal}\n\n"; } $orderContent .= "Total Amount: €{$total}\n"; $orderContent .= "Order Time: " . date('Y-m-d H:i:s') . "\n"; // Mail configuration $to = "techologyy@gmail.com"; // Your business email $subject = "New Customer Order"; $from = "techologyy@gmail.com"; // Sender email (match your domain if possible) // Required headers to avoid spam filters $headers = "From: {$from}\r\n"; $headers .= "Reply-To: {$from}\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; // Send email and handle result $mailSent = mail($to, $subject, $orderContent, $headers); if($mailSent){ unset($_SESSION['cart']); // Empty cart after successful checkout echo "<script> alert('Checkout successful! Thank you for your purchase.'); window.location='index.php'; </script>"; }else{ echo "<script> alert('Oops! There was an error processing your order. Please try again later.'); window.location='cart.php'; </script>"; } ?>
Extra notes
- If testing locally (e.g., XAMPP/WAMP), you'll need to configure PHP's mail settings (like sendmail or SMTP) to send emails successfully.
- For production, consider using a reliable library like PHPMailer instead of the native
mail()function—it handles SMTP authentication better and reduces the chance of emails being marked as spam.
内容的提问来源于stack exchange,提问作者Redent

