0

I'm building shopping web application. the thing that I want is, when user click Add to cart's button, the quantity order will update instantly on the shopping/product.php page

My Update quantity order's logic is in the "add method" controller page.

public function add()
    {
              // Set array for send data.
        $insert_data = array(
            'id' => $this->input->post('id'),
            'name' => $this->input->post('name'),
            'price' => $this->input->post('price'),
            'qty' => 1
        );      

                 // This function add items into cart.
        $this->cart->insert($insert_data);

                // This will show insert data in cart.
        $this->load->view('header');
        $this->load->view('shopping/cart');
        $this->load->view('footer');
         } 

assume I have 2 "view" page

  • shopping/product.php - the product list page. this page will submit the button to controller "add" but I use ajax trick. so it will stay on the same page
  • shopping/cart.php this page is in the controller add and this is the page that start session. this page I contain the quantity of order data to Session

In shopping/product.php I write function onclick to button(add to cart) and I add the session from shopping/cart.php in this text.

 $(document).ready(function(){       
      $('.add-to-cart').on('click',function(){

        document.getElementById('outside_cart_text').innerHTML = 
"Qty type <?php echo $this->session->userdata('qty_type'); ?> amount <?php echo $this->session->userdata('qty_product');?>";

            });
        }); 

after I click, it change the original text to "Qty type amount". But session value not appear.

the question is how to make it appear instantly?

additional detail : I use this trick in shopping/product.php page. It work like ajax. so after click I still in the same page (and not reload)

<style>
  .hide { position:absolute; top:-1px; left:-1px; width:1px; height:1px; }
</style>

<iframe name="hiddenFrame" class="hide"></iframe>

<form action="receiver.pl" method="post" target="hiddenFrame">
  <input name="signed" type="checkbox">
  <input value="Save" type="submit">
</form> 
Supanat T. Boss
  • 305
  • 4
  • 13
  • 1
    The way I see your workflow is this. Load Product page, Hit button, Ajax hits Cart. If that's the case, your php variables on the product page won't be updated because they were evaluated and output when you loaded the Product Page. It might be better to return the value through your Ajax request and use that value to update the page. – Luke Oct 25 '16 at 05:44
  • 1
    Check out [this question](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) for more information on getting callbacks from Ajax – Luke Oct 25 '16 at 05:46

0 Answers0