• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

How to make all items of my shopping cart legit for being updated?

Hi, I have a shopping cart that I am trying to update:

(add stuff first)

http://www.auroriella.com/bracelet.php?item=b3
http://www.auroriella.com/bracelet.php?item=b4

shopping cart:

http://www.auroriella.com/show_cart.php

well it only updates the first item in the shopping cart, bc its only pulling the first row of stuff from it

function updateCart() {
	if((isset($_POST['update'])) && (!empty($_POST['qty'])) && (!empty($_POST['item']))) {
		$item = trim(mysql_clean_strings($_POST['item']));
		var_dump($item);
		echo "<p>";
		$qty = trim(mysql_clean_strings($_POST['qty']));
		var_dump($qty);
		echo "<p>";
		$cart = $_SESSION['cart'];
		$cart[$item] = $qty;
		var_dump($cart);
	}
}

Open in new window


How do I pull everything from the form??
0
FairyBusiness
Asked:
FairyBusiness
  • 8
  • 6
1 Solution
 
hieloCommented:
BEFORE:
$cart = $_SESSION['cart'];

put:
$_SESSION['cart'][$item]=$qty;
0
 
FairyBusinessAuthor Commented:
nope its still just updating the first one.  I think because its only pulling one item and one qty from the form?
0
 
hieloCommented:
in show_cart.php you have the following html:
<input name="item" ...> <input name="quantity" ... > <input name="delete" ...>

What you need to is make "php arrays":

<input name="item[]" value="b3" > <input name="quantity[b3]" ... > <input name="delete[b3]" ...>

Then try:

function updateCart() {
	if((isset($_POST['update'])) && (!empty($_POST['qty'])) && (!empty($_POST['item']))) {
                /*
		$item = trim(mysql_clean_strings($_POST['item']));
		var_dump($item);
		echo "<p>";
		$qty = trim(mysql_clean_strings($_POST['qty']));
		var_dump($qty);
		echo "<p>";
		$cart = $_SESSION['cart'];
		$cart[$item] = $qty;
		var_dump($cart);
                */
                foreach($_POST['item'] as $item)
                {
                   $item=trim(mysql_clean_strings($item));
                   $qty=trim(mysql_clean_strings($_POST['quantity'][$item]));
                   $_SESSION['cart'][$item]=$qty;
                }
                echo '<pre>'.print_r($_SESSION['cart'],true).'</pre>';
	}
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
FairyBusinessAuthor Commented:
I got this back:

Notice: Undefined index: quantity in /hermes/web09c/b2950/moo.auroriellacom/includes/library.php on line 422

Array
(
    [b3] =>
    [b4] => 1
)


also it got rid of my b3 qty!
0
 
FairyBusinessAuthor Commented:
oh I forget to change quantity to qty.  I just did and its back to where we were before.  It updates the first one only
0
 
hieloCommented:
it seems to work ok for me!
0
 
FairyBusinessAuthor Commented:
You added both of these to the shopping cart?

http://www.auroriella.com/bracelet.php?item=b3
http://www.auroriella.com/bracelet.php?item=b4

and then tried to update both?  bc its only letting me update the top one. ..
0
 
hieloCommented:
>>$_POST['quantity'][$item]
should be $_POST['qty'][$item]
0
 
FairyBusinessAuthor Commented:
This is what I have:

function updateCart() {
	if((isset($_POST['update'])) && (!empty($_POST['qty'])) && (!empty($_POST['item']))) {
                /*
		$item = trim(mysql_clean_strings($_POST['item']));
		var_dump($item);
		echo "<p>";
		$qty = trim(mysql_clean_strings($_POST['qty']));
		var_dump($qty);
		echo "<p>";
		$cart = $_SESSION['cart'];
		$cart[$item] = $qty;
		var_dump($cart);
                */
                foreach($_POST['item'] as $item)
                {
                   $item=trim(mysql_clean_strings($item));
                   $qty=trim(mysql_clean_strings($_POST['qty'][$item]));
                   $_SESSION['cart'][$item]=$qty;
                }
                echo '<pre>'.print_r($_SESSION['cart'],true).'</pre>';
	}
}

Open in new window

0
 
hieloCommented:
you are sending one <FORM> per row. The browser can submit only one form at a time.  You need to use ONE <form> tag and it needs to be "Wrapped" around your entire table.
0
 
FairyBusinessAuthor Commented:
I put it like that for the delete buttons. . .  I'm not sure how else to delete otherwise!
0
 
hieloCommented:
if you have <input name="delete['b3]" type="submit" value="Delete">

when you press it, you will be able to see/detect delete['b3'] ($_POST['delete']['b3']). When you detect it, delete it!

And stop drowning in a glass of water!
0
 
FairyBusinessAuthor Commented:
It works  :))  Thanks!
0
 
FairyBusinessAuthor Commented:
ok maybe you can help me with my next question:  removing an item from the shopping cart
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now