I want to add multile items toshopping cart

Discussion in 'PHP' started by countrydj, Jun 21, 2012.

  1. #1
    Hi Guys..
    I am setting a shopping cart and it works OK with BUY NOW buttons and adds the product to the cart one at a time.

    However, I want to select a few items, via checkboxes, and pass them to the cart.php all at the same time. The shopping cart has facility to change the quantities and update the cart.

    This is a chopped down version of the sending code:
    		$main_content .= "<form action='cart/cart.php?action=add&id=$id' method='get' target='new'>";
    		if($color==1){
    		$main_content .= "<tr bgcolor='#ffcc99'>";
    // ORDERS 1
    		$main_content .= "<td valign='top'>$product <font color=#ff0000><b>$comments</b></font></td>";
    		$main_content .= "<td valign='top'>$packquantity</td>\n";
    		$main_content .= "<td valign='top'>&pound$trade</td>\n";
    
    		$main_content .= "<td valign='top'><input type='checkbox' name='id' value='$id'>\n";
     
    		$main_content .= "</tr>\n";
    
    
    		$main_content .= "<input type='submit' value='Place Order'>";
    		$main_content .= "&nbsp;&nbsp;&nbsp;&nbsp;<input type='reset' value='Clear'>";
    
    		$main_content .= "</form></form></td></tr></table>\n";
    
    Code (markup):
    The receiving code in my cart.php file is:
    $cart = $_SESSION['cart'];  
    $action = $_GET['action'];  
    
    switch ($action) {  
        case 'add': 
            if ($cart) {  
                $cart .= ','.$_GET['id'];  
            } else {  
                $cart = $_GET['id'];  
            }  
    header("Location: cart.php"); 
            break;  
    Code (markup):
    I think that the receiving code is wrong. It works OK for the BUY NOW button, one at a time, but it doesn't work for multiple additions.

    Can anybody help ???

    Thanks,

    John C
     
    countrydj, Jun 21, 2012 IP
  2. AustinQPT

    AustinQPT Member

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    
    "<form action='cart/cart.php?action=add&id=$id' method='get' target='new'>";
    
    PHP:
    should be
    
    "<form action='cart/cart.php?action=add&id=".$id."' method='get' target='new'>";
    
    PHP:
    and

    
    $main_content .= "</form></form></td></tr></table>\n";
    
    PHP:
    should be
    
    $main_content .= "</form></td></tr></table><br>";
    
    PHP:
    also make sure when passing the items in the $id variable you make sure not to be passing special characters ex: '?&/\"
    if you need those characters to be passed consider using POST rather than GET

    Hope this helps and good luck
     
    AustinQPT, Jun 24, 2012 IP
  3. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #3
    You're correct that your cart.php script is not designed to add multiple items to the shopping cart, and would need to be re-written to make it possible. But without seeing the entire script, it's impossible to suggest replacement code.
     
    rainborick, Jun 24, 2012 IP
  4. countrydj

    countrydj Active Member

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #4
    Hi Guys...

    Thank you for taking the time to respond to my problems.
    However, I have now fixed it.
    If you are interested, here is what fixed it:
    This is my original index.php code:
    		$main_content .= "<form action='cart/cart.php?action=add&id=$id' method='get' target='new'>";
    		if($color==1){
    		$main_content .= "<tr bgcolor='#ffcc99'>";
    // ORDERS 1
    		$main_content .= "<td valign='top'>$product <font color=#ff0000><b>$comments</b></font></td>";
    		$main_content .= "<td valign='top'>$packquantity</td>\n";
    		$main_content .= "<td valign='top'>&pound$trade</td>\n";
    
    		$main_content .= "<td valign='top'><input type='checkbox' name='id' value='$id'>\n";
     
    		$main_content .= "</tr>\n";
    
    
    		$main_content .= "<input type='submit' value='Place Order'>";
    		$main_content .= "&nbsp;&nbsp;&nbsp;&nbsp;<input type='reset' value='Clear'>";
    
    		$main_content .= "</form></form></td></tr></table>\n";
    Code (markup):
    This is what had to change it to:
    		$main_content .= "<form action='cart/cart.php' method='get' target='new'>";
    		if($color==1){
    		$main_content .= "<tr bgcolor='#ffcc99'>";
    // ORDERS 1
    		$main_content .= "<td valign='top'>$product <font color=#ff0000><b>$comments</b></font></td>";
    		$main_content .= "<td valign='top'>$packquantity</td>\n";
    		$main_content .= "<td valign='top'>&pound$trade</td>\n";
    
    		$main_content .= "<td valign='top'><input type='checkbox' name='id' value='$id'>\n";
     
    		$main_content .= "</tr>\n";
    
    //############### I HAD TO ADD THIS NEXT LINE #####################
    		$main_content .= "<input type='hidden' name='action' value='add'></input>";
    //############### NO OTHER COMBINATION WOULD WORK ##############
    
    		$main_content .= "<input type='submit' value='Place Order'>";
    		$main_content .= "&nbsp;&nbsp;&nbsp;&nbsp;<input type='reset' value='Clear'>";
    
    		$main_content .= "</form></form></td></tr></table>\n";
    Code (markup):
    The next part was the cart.php:
    $cart = $_SESSION['cart'];  
    $action = $_GET['action'];  
    
    $tmp='';
    switch ($action) {  
        case 'add': 
            if ($cart) {  
                $cart .= ','.$_GET['id'];  
            } else {  
                $cart = $_GET['id'];  
            }  
    header("Location: cart.php"); 
            break;
    Code (markup):
    This was the final solution:
    
    $cart = $_SESSION['cart'];  
    $action = $_GET['action'];  
    
    
    switch ($action) {  
        case 'add': 
    
    
            foreach($_GET['id'] as $id){
                   $tmp .=','.$id;
            }
    
            if ($cart) {  
                $cart .= ','.$tmp;  
            } else {  
                $cart = $tmp;  
            }  
    
    	header("Location: cart.php"); 
    
    //##### This next line was added because I was getting a spurious item at the begining of he cart. On checking, I found that the passed array was '2'3'4'5
    The first , was causing the spurious item. Removing the firs character from the array solved it.############
    
    $cart = substr($cart, 1); 
    
    //#################################################
    
            break;  
    Code (markup):
    If you're not intersted, it may be of some help to others who may be struggling.
    Thanks again,

    John C
     
    countrydj, Jun 25, 2012 IP