Welcome Guest, Not a member yet? Register   Sign In
Updating a cart with AJAX
#1

[eluser]DocFunky![/eluser]
Aloha

I would really need some help from to AJAX Guru master overthere to help me building my update cart function on my website in AJAX.

So basically, what I would like to do is, when I modify one of my product in one input_dropdown, my 'update_cart' function is automaticaly called and my prices are updated as well as my input

Here is my view :
Code:
<?php
          $options = array(
             '0'  => '0',
             '1'  => '1',
             '2'  => '2',
             '3'  => '3',
             '4'  => '4',
             '5'  => '5',
             '6'  => '6',
             '7'  => '7',
           );
          
           if($product['quantity']==0){
            $value[$product['title']] = set_value('quantity'.$product['title']);
           }else{
            $value[$product['title']] = $product['quantity'];
           }
           $data0 = 'class="quantSelect" value="'.$value[$product['title']].'" id="quant'.$product['title'].'"';
          
           echo  form_dropdown('quantity'.$product['title'], $options, $value[$product['title']],$data0);
         ?>
        </td>
        <td>
         &lt;?php echo $product['price'] ?&gt;
        </td>
        <td id="&lt;?php echo 'price'.$product['title']?&gt;">
         $&lt;?php echo $total[$product['title']] ?&gt;
        </td>

Well, everything is in a foreach loop but I think that here, it doesn't matter.

Then I tried to set up that AJAX function :

Code:
$(".quantSelect").click(function(){  
    $.POST("&lt;?php echo base_url().'main/update_cart';?&gt;",  
           {product_id:$('&lt;?php echo $product['quantity']; ?&gt;').val(),quantity:$('&lt;?php echo 'quantity'.$product['title'] ?&gt;').val()},  
           function(data){  
                if(data.success) {  
                     $("&lt;?php echo 'price'.$product['title']?&gt;").val(data.some_returned_value); // update value of an text input or textarea (view more info about jQuery selectors)
                     $("#totalPriceWithTaxes").html(data.some_other_returned_value); // update value of a paragraph                    
                }
           }, 'json');
});

And at last my function :

Code:
function  update_cart(){
$success = false;
  if(!empty($_POST['product_id']) && !empty($_POST['quantity']) && is_numeric($_POST['quantity'])) {  

   // I get all the information i need here in order to calcul the final price
   //We calcul the final price with taxes, shipping and everything.
   $data['totalPriceWithTaxes'] = $data['tax'] + $data['totalPrice'] + $data['Shipping']->shipping;
   $this->session->set_userdata('totalPriceWithTaxes', $data ['totalPriceWithTaxes']);
    
   $success = true;
   $some_returned_value = 69;
   $some_other_returned_value = $data['totalPriceWithTaxes']; // the final price
  }
echo json_encode(array("success" => $success,
      "some_returned_value" => $some_returned_value,
      "some_other_returned_value" => $some_other_returned_value));

}

Here we are, so it doesn't work at all ... Sad I can't see any update. If someone could help me to figure out how to set up that AJAX Function, I would deeply appreciate Smile
#2

[eluser]DocFunky![/eluser]
Please, I really need your help on that Smile




Theme © iAndrew 2016 - Forum software by © MyBB