根据jquery所做的更改更新会话变量

时间:2013-11-06 09:11:00

标签: php jquery ajax

您好我正在开发购物车。向客户展示了客户订购的产品。所有产品及其详细信息都存储在会话变量中。如果他愿意,他可以改变每种产品的数量。当他改变产品的数量时,相应的价格会发生变化。我已经使用了jquery。现在我需要在更改数量时更新会话变量。我也必须展示它。 这是我的代码。

 <?php
  session_start();
   include('head.php');


      ?>
  <!DOCTYPE HTML>
   <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>  
          <script>
     $(document).ready(function() {   
    $(".myclass").change(function(){
        var identifier = $(this).attr('id');
        var Qty = $(this).val();
        var Price = $("#price_"+identifier).val();//price value
        var Total =  Qty * Price;  
        $("#priceDisplay_"+identifier).html(Total);                 
        GrandTotal();
       });
     GrandTotal();      
      });

    function GrandTotal(){
    var GrandTotal=0;
    var cart=0;
     $(".myclass").each(function(){
         var identifier = $(this).attr('id');
         var Qty = $(this).val();    
         var Price = $("#price_"+identifier).val();//price value
         cart +=parseInt(Qty);
         var Total =  Qty * Price; 

         GrandTotal += Total;
         $("#priceDisplay_"+identifier).html(Total); 

       });
     $("#cart").html(cart);
     $("#GrandTotal").html(GrandTotal);

    }

     function RemoveCart(ob){
     if (confirm("Are you sure to Remove?"))
     {    
     var IdForRemove =ob.value;

        $.ajax({
         type: "POST",
         url: "RemoveCart.php",
         dataType:'json',
         data: { id: IdForRemove}
         })
         .done(function( data ) {
             $("#GrandTotal").html(data.Grandtotal);                    
             $("#cart").html(data.cart);
             $("#"+IdForRemove).remove();

         });

        return false;

        }
      }

       </script>
    </head> 
   <body>


     <?php
       if(empty($_SESSION['items'])){ ?>
     <center><b><font color="red">There are no products in your cart!!</font></b> </center>    
         <?php  
       }
       else

       {
       ?>

    <b><font color="#0000A0"> Your Shopping Cart!!</font></b><br><br>
    <form name="formview" action="orderform.php" method="post">
   <?php $count=0; 
   ?>


   <table  id="mytable" width="50%" cellpadding="1px" cellspacing="3px" border="1" bgcolor="#BDEDFF">
    <tr>  
    <th>Item</th>
    <th>Price</th>
    <th>Quantity</th>
    <th>Subtotal</th>
     <th>Remove</th>
    </tr> 
   <?php  
   $cnt=0; 
   $CartR = $_SESSION['r'];
   foreach( $CartR as $key=>$ar):
   $Identifier = 'qty_'.$cnt;  

    ?>    <tr id="<?php echo $ar['Id']; ?>">

      <td align="center"><?php echo $ar['Name']?></td>
      <td align="center"><?php echo $ar['Price']?></td>
      <td align="center">
      <select class="myclass" name="qty" id='<?php echo $Identifier;?>' >
     <?php for ($i=1; $i<=100; $i++) {
           $y="";
           if( $i==$_SESSION['r'][$key]['Quantity']){
            $y="selected";
            }
            echo "<option ";
            echo "value=\"$i\" ".$y.">", $i, "</option>\n";


        } ?>

     </select>
     </td>

    <?php $x=$x+$ar['Quantity']; ?>

       <td name="price"><span id="priceDisplay_<?php echo $Identifier;?>"><?php echo $ar['Total']; ?></span></td>          
      <input type='hidden' name='id' id="pid_<?php echo $Identifier;?>" value='<?php echo $ar['Id']; ?>'>
      <input type='hidden' name='price' id="price_<?php echo $Identifier;?>" value='<?php echo $ar['Price'] ?>' class="input">  
    <td>
      <input type="checkbox" onclick="RemoveCart(this)" value="<?php echo $ar['Id']; ?>"> 

    <?php   $tt[]=$ar['Total']; ?>
    </tr>
    <?php
   $cnt++;     
   endforeach;
     ?>  </table>
     <br><br>
     <table>
     <tr></tr>
        <tr><b><font color="B048B5">Grand Total :</font></b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="GrandTotal"></span></tr>
        <b><!--<font color="#F6358A">Your Cart:--></font></b><div align="center" id="cart"></div> 
      <center>  <img src="upload/images.jpeg" width="100"></center>
      </table>


      <!--<table align="right">-->
        <?php foreach($tt as $t)
      {
         $count=$count+$t;
            }

        ?>  
   <br>
  <br>

     <!--<b><div id="cart">Your Cart:</div></b> -->  

   <?php
     }       
       if(!empty($_SESSION['r']))
      {  ?>

           <input type="submit" name="checkout" value="CheckOut">
        <?php } ?>
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    <a href="ebazaar.php"><b><font  color="#7D0541">Back</font></b></a>


       </form>
       </body>

我使用jquery进行的更改也应该更改我的php变量。

1 个答案:

答案 0 :(得分:0)

你能试试吗?

添加了UpdateCart功能,

         function UpdateCart(Qty, IDCart){             

             $.ajax({
                 type: "POST",
                 url: "UpdateCart.php",
                 dataType:'json',
                 data: {id:IDCart, Qty: Qty}
                 })
                 .done(function( data ) {

                 });

             return false;
          }


          $(".myclass").change(function(){
                 var identifier = $(this).attr('id');
                 var Qty = $(this).val();
                 var Price = $("#price_"+identifier).val();//price value
                 var Total =  Qty * Price;                   
                 $("#priceDisplay_"+identifier).html(Total);  
                 var IDCart = $("#pid_"+identifier).val();//passing reference value
                 UpdateCart(Qty, IDCart);//added this for update qty               
                 GrandTotal();
               });
             GrandTotal();       

            });

创建一个新文件:UpdateCart.php

     <?php session_start();
            if(isset($_POST['id'])){

                $toBeUpdate =$_POST['id'];
                $CartR =$_SESSION['r'];

                $Quantity = $_POST['Qty'];
                    foreach($CartR as $key=>$Cart){                     

                        if($Cart['Id']==$toBeUpdate){                   

                            $_SESSION['r'][$key]['Quantity']=$Quantity;

                        }
                    }


                    echo json_encode(array('success'=> true));
            }


        ?>