hmbargi
asked on
How to delete an item from shopping cart?
Hi,
I would like to delete an item from the shopping cart but the it doesn't work.
Here is the item:
<td align=\"center\"> <input type=\"submit\" value=\"Remove\" name=\"delete[b3]\" class=\"submit\">
and the the function:
Thanks!
I would like to delete an item from the shopping cart but the it doesn't work.
Here is the item:
<td align=\"center\"> <input type=\"submit\" value=\"Remove\" name=\"delete[b3]\" class=\"submit\">
and the the function:
function removeItem() {
if(isset($_POST['delete']) ) {
foreach($_POST['delete'] as $item =>$v ) {
$item = trim(mysql_clean_strings($item));
unset($_SESSION['cart'][$item]);
}
}
}
Thanks!
Need more info. How are your items shown on the page in the form you are submitting?
ASKER
Here is the function for displaying the items in the cart:
function display_cart($cart, $change = true, $images = 1) {
// display items in shopping cart
// optionally allow changes (true or false)
// optionally include images (1 - yes, 0 - no)
echo "<table border=\"0\" width=\"100%\" cellspacing=\"0\">
<form action=\"show_cart.php\" method=\"post\">
<tr><th colspan=\"".(1 + $images)."\" bgcolor=\"#cccccc\">Item</th>
<th bgcolor=\"#cccccc\">Price</th>
<th bgcolor=\"#cccccc\">Quantity</th>
<th bgcolor=\"#cccccc\">Total</th>
<th bgcolor=\"#cccccc\">Remove</th>
</tr>";
//display each item as a table row
foreach ($cart as $itemid => $qty) {
$item = get_item_details($itemid);
echo "<tr>";
if($images == true) {
echo "<td align=\"left\">";
if (file_exists("images/".$itemid.".jpg")) {
$size = GetImageSize("images/".$itemid.".jpg");
if(($size[0] > 0) && ($size[1] > 0)) {
echo "<img src=\"images/".$itemid.".jpg\"
style=\"border: 1px solid black\"
width=\"".($size[0]/3)."\"
height=\"".($size[1]/3)."\"/>";
}
} else {
echo " ";
}
echo "</td>";
}
echo "<td align=\"left\">
<a href=\"show_item.php?itemid=".$itemid."\">".$item['name']."</a>
</td>
<td align=\"center\">\$".number_format($item['price'], 2)."</td>
<td align=\"center\">";
// if we allow changes, quantities are in text boxes
if ($change == true) {
echo "<input type=\"text\" name=\"".$itemid."\" value=\"".$qty."\" size=\"3\">";
} else {
echo $qty;
}
echo "</td><td align=\"center\">\$".number_format($item['price']*$qty,2)."</td>
<td align=\"center\"> <input type=\"submit\" value=\"Remove\" name=\"delete[b3]\" class=\"submit\">
</td>
</tr>\n";
}
// display total row
echo "<tr>
<th colspan=\"".(2+$images)."\" bgcolor=\"#cccccc\"> </td>
<th align=\"center\" bgcolor=\"#cccccc\">".$_SESSION['items']."</th>
<th align=\"center\" bgcolor=\"#cccccc\">
\$".number_format($_SESSION['total_price'], 2)."
</th>
<th align=\"center\" bgcolor=\"#cccccc\"></th>
</tr>";
// display save change button
if($change == true) {
echo "<tr>
<td colspan=\"".(2+$images)."\"> </td>
<td align=\"center\">
<input type=\"hidden\" name=\"save\" value=\"true\"/>
<input type=\"image\" src=\"images/save-changes.gif\"
border=\"0\" alt=\"Save Changes\"/>
</td>
<td> </td>
</tr>";
}
echo "</form></table>";
}
Any relevant error messages in the http server's log file?
I suggest a var_dump of all the variables you are using ( $_POST, $_SESSION ...). I would look at $item before and after trimming.
You are looking for anything that surprises you.
I suggest a var_dump of all the variables you are using ( $_POST, $_SESSION ...). I would look at $item before and after trimming.
You are looking for anything that surprises you.
What is the shopping cart that you are using? I don't recognize the code.
Need to also see code for show_cart.php to see how the delete is handled.
ASKER
hmccurdy: changing the variable doesn't work.
Ray_Paseur: Don't know what do you mean.
EddieShipman: Here is the shoe_cart.php code
Ray_Paseur: Don't know what do you mean.
EddieShipman: Here is the shoe_cart.php code
<?php
include ('hbargi_fns.php');
// The shopping cart needs sessions, so start one
session_start();
@$new = $_GET['new'];
if($new) {
//new item selected
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
$_SESSION['items'] = 0;
$_SESSION['total_price'] ='0.00';
}
if(isset($_SESSION['cart'][$new])) {
$_SESSION['cart'][$new]++;
} else {
$_SESSION['cart'][$new] = 1;
}
$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items($_SESSION['cart']);
}
if(isset($_POST['save'])) {
foreach ($_SESSION['cart'] as $itemid => $qty) {
if($_POST[$itemid] == '0') {
unset($_SESSION['cart'][$itemid]);
} else {
$_SESSION['cart'][$itemid] = $_POST[$itemid];
}
}
$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items($_SESSION['cart']);
}
do_html_header("Your shopping cart");
if(($_SESSION['cart']) && (array_count_values($_SESSION['cart']))) {
display_cart($_SESSION['cart']);
} else {
echo "<p>There are no items in your cart</p><hr/>";
}
// get the item info out from
$target = "index.php";
// if we have just added an item to the cart, continue shopping in that country
if($new) {
$details = get_item_details($new);
if($details['catid']) {
$target = "show_cat.php?catid=".$details['catid'];
}
}
display_button($target, "continue-shopping", "Continue Shopping");
// use this if SSL is set up
// $path = $_SERVER['PHP_SELF'];
// $server = $_SERVER['SERVER_NAME'];
// $path = str_replace('show_cart.php', '', $path);
// display_button("https://".$server.$path."checkout.php",
// "go-to-checkout", "Go To Checkout");
// if no SSL use below code
display_button("checkout.php", "go-to-checkout", "Go To Checkout");
do_html_footer();
?>
What is the shopping cart that you are using? By that I mean, what is the name of the shopping cart. Like Magento, UberCart, etc.
ASKER
I'm not using any outsource shopping cart. I'm building my own cart.
The POST array as if you are submitting it contains nothing. the name for the submit does not post the b3 value to the target form. Do you have any other input fields that have values ?
ASKER
Since I'm newbie to php, do I have to include some code to show_cart.php or just deal with submit button and create a function that calls it.
Since you are a newbie, I'd recommend doing it step by step with a simple tutorial - having a ready made functions may be a little confusing at this point, build it by yourself, you'll learn a lot.
http://v3.thewatchmakerproject.com/journal/276/
http://v3.thewatchmakerproject.com/journal/276/
I wasn't trying to get you to change the variables. I want you to print them out and share any answers that don't make sense to you.
I don't see that you are handling the delete submit button in the show_cart.php file at all. Try something like this:
// Handle deleting the item
if(isset($_POST['delete[b3]'])) {
removeItem($_SESSION['cart'][$item); // Then modify the removeItem function to take the item as a parameter
}
do_html_header("Your shopping cart");
ASKER
EddieShipman: Tried your code. Doesn't work!
ASKER
Do I have to create a table in DB for the items in the cart?
Do I have to create a table in DB for the items in the cart?
No. But I strongly suggest that you do.
No. But I strongly suggest that you do.
ASKER
Still need help guys.
What approach do you want to take? Are you going to go with my suggestion and add the cart to a DB or do you want to stick with session variables?
ASKER
Either ways fine with me. I created the order_items table but still couldn't figure out how to implement the code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Not what I expected. I got a provided a code so Nobody was able to fix it.