0

Hi I am developing a shopping cart.The products ordered by a customer is shown to him. All the products and their details are stored in a session variable. He can change the quantity of each product, if he wants to.When he changes the quantity of a product,corresponding price changes.This I have done using jquery. Now I need to update the session variable,when he changes the quantity. And I have to display it also.

Here is my code.

<?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>

Anyone please help me. The change I made using jquery should also change my php variables.

This is my updated 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;
         }
        }

here pid is my product_id passed from ajax.Please help me

1 Answer 1

1

If you want to update your php variable then you can do so via ajax.Try to do like this:

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

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;
           echo $_SESSION['r'][$key]['Quantity'];
           echo $_SESSION['r'][$key]['Total'];
           }
        }
Sign up to request clarification or add additional context in comments.

5 Comments

BUT JOKE how can I get the quantity and price values in sessionupdate.php?Can you please help me with the code.
but joke I did it. But nothing is displaying on the page.Only the table heading is displayed.I will post my code.Please check it.
Because your not echo anythin..In your php page echo something..and in the ajax success function alert data..
I tried it.When I tried to alert data.prce,' undefined; is alerted.
Please help me joke.What will be the reason?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.