0

您好我正在开发一个购物车。客户订购的产品显示给他。所有产品及其详细信息都存储在一个session变量中。如果他愿意,他可以更改每个产品的数量。当他更改产品的数量时,相应的价格也会发生变化。这个我已经使用了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 变量。

这是我更新的 sessionupdate.php

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

       $qnty=$_GET['quantity'];
      $prce=$_GET['price1'];
        $_SESSION['r']=$_GET['id'];
       $pid=$_GET['pid'];
          foreach ($_SESSION['r'] as $key => $value) {
         if($key==$pid){ 
       $_SESSION['r'][$key]['Quantity']=  $qnty;
           $_SESSION['r'][$key]['Total']=  $prce*$qnty;
         }
        }

这里pid是我从ajax传递的product_id。请帮助我

4

1 回答 1

1

如果你想更新你的php变量,那么你可以通过 ajax 进行。尝试这样做:

$(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();

        //Call an ajax function here
          $.ajax({
             type: "GET",
             url: "sessionupdate.php",
             data: { quantity:Qty,price1:Price,id: <?php echo $_SESSION['r'];?>},
             success:function(data){
                     alert(data);
              }
           });
       });
     GrandTotal();      
      });

会话更新.php:

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

      $qnty=$_GET['quantity'];
      $prce=$_GET['price1'];
      $_SESSION['r']=$_GET['id'];
      $pid=$_GET['pid'];
       foreach ($_SESSION['r'] as $key => $value) {
         if($key==$pid){ 
           $_SESSION['r'][$key]['Quantity']=  $qnty;
           $_SESSION['r'][$key]['Total']=  $prce*$qnty;
           echo $_SESSION['r'][$key]['Quantity'];
           echo $_SESSION['r'][$key]['Total'];
           }
        }
于 2013-11-06T06:47:06.793 回答