how to edit value in session in PHP

Viewed 132

Am trying to edit quantity in my shopping cart using session in PHP but all the program is doing is changing the quantity of the first array. i have tried troubleshooting without any success.

my html

<div id="shopping-cart">

                       <?php
                        if(isset($_SESSION["cart_sales"])){
                            $itemsale_total = 0;
                        ?>  
                        <table class="">

                        <tr>

                        <th ><strong>Product Name</strong></th>
                        <th ><strong>Quantity</strong></th>
                        <th ><strong>Price (Kshs)</strong></th>


                        <th ><strong>Amount</strong></th>

                        <th ><strong>Save&nbsp;&nbsp;&nbsp;&nbsp;Delete</strong></th>

                        </tr>   
                        <?php       
                            foreach ($_SESSION["cart_sales"] as $itemsale){
                                ?>


                        <tr>

                        <td width="240px"><?php echo $itemsale["productname"]; ?></td>
                        <td width="60px" align="center">


                        <textarea   rows="1"  maxlenth = "20" class="round"  id="<?php echo $itemsale["productname"]; ?>"   name="quantityc" onBlur="saveCart(this);"><?php echo $itemsale["quantity"]; ?></textarea>
                        </td>
                        <td ><textarea   rows="1"  maxlenth = "20" class="round" readonly   id="sessitemsale" name="sessitemsale" style="" ><?php echo $itemsale["price"]; ?></textarea></td>


                        <td><textarea   rows="1"  maxlenth = "20" class="round"    id="sesspricequantity" name="sesspricequantity" style="" ><?php echo "Kshs. ".($itemsale["price"]*$itemsale["quantity"]); ?></textarea>

                        </td>

                         <td align="center"><?php echo '<span class="remove-itm"><a href="add_sales.php?saveep='.$itemsale["productname"].'&return_url='.'add_sales.php'.'"><img src="images/saveicon.png" width="30" height="30" /></a></span>'; ?>

                         &nbsp;&nbsp;&nbsp;&nbsp;

                         <?php echo '<span class="remove-itm"><a href="add_sales.php?removep='.$itemsale["productname"].'&return_url='.'add_sales.php'.'"><img src="images/close_new.png" width="30" height="30" /></a></span>'; ?>

                         </td>
                        </tr>



                                        <?php
                                $itemsale_total += ($itemsale["price"]*$itemsale["quantity"]);
                                }
                                ?>

                         </table>
                          </div>
                     </div>

Ajax

 <script>
                    function saveCart(obj) {
                        var quantityc = $(obj).val();
                        var code = $(obj).attr("id");
                        $.ajax({
                            url: "?action=edit",
                            type: "POST",
                            data: 'code='+code+'&quantityc='+quantityc,
                            success: function(data, status){$("#paymentamount").html(data)},
                            error: function () {alert("Problen in sending reply!")}
                        });
                    }
                    </script>   

PHP

if(!empty($_GET["action"])) {
    switch($_GET["action"]) {   
        case "edit":
            $total_price = 0;
            foreach ($_SESSION['cart_sales'] as $itemsale => $v) {

              if($_POST["code"] == $itemsale) {

                  echo $_POST["code"];
                  if($_POST["quantityc"] == '0') {
                      unset($_SESSION["cart_sales"][$itemsale]);
                  } else {
                    $_SESSION['cart_sales'][$itemsale]["quantity"] = $_POST["quantityc"];
                  }
              }
              $total_price += $_SESSION['cart_sales'][$itemsale]["price"] * $_SESSION['cart_sales'][$itemsale]["quantity"]; 

            }
            if($total_price!=0 && is_numeric($total_price)) {
                print  number_format($total_price,2);
                exit;
            }
        break;  
    }
}

Any help to solve this problem will be highly appreciated

0 Answers
Related