如何在PHP中编辑会话中的值

时间:2016-03-12 12:37:05

标签: php jquery arrays ajax session-variables

我尝试使用PHP中的会话编辑购物车中的数量,但所有程序正在做的是更改第一个数组的数量。我试过没有成功的故障排除。

我的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;  
    }
}

任何有助于解决此问题的帮助都将受到高度赞赏

0 个答案:

没有答案