session php with ajax innerHTML shopping cart's quantity order

雨燕双飞 提交于 2020-01-17 05:21:11

问题


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> 

来源:https://stackoverflow.com/questions/40231791/session-php-with-ajax-innerhtml-shopping-carts-quantity-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!