Codeigniter Jquery:我想重新加载这个页面

时间:2017-02-28 07:08:50

标签: javascript jquery codeigniter cart

我想将产品添加到购物车并重新加载同一页面时,但问题不在于此产品。

控制器

public function detail()
 {              
      $data=array('title' =>'Ecommerce Online | Detail Product',
           'username'   => $this->session->userdata('id'),
           'categories' => $this->categories_model->get_categories(),
           'details'    => $this->categories_model->get_details($this->uri->segment(3)),
           'isi'        =>'member/detail');
      $this->load->view('template_home/wrapper',$data);
 }

function addtocart()
{                   
    if($this->cart_model->validate_add_cart_item() == TRUE){
        if($this->input->post('ajax') != '1'){
            redirect('member/detail/'); // this problem
        }else{
            echo 'true';
        }
        }
}

我添加了我的模型

function validate_add_cart_item()
{
        $id = $this->input->post('product_id'); 
        $cty = $this->input->post('quantity'); 

        $this->db->where('productID', $id);
        $query = $this->db->get('product', 1);

        if($query->num_rows > 0){

            foreach ($query->result() as $row)
            {
                $data = array(
                    'id'      => $id,
                    'qty'     => $cty,
                    'price'   => $row->price,
                    'name'    => $row->productName
                );

                $this->cart->insert($data); 

                return TRUE;
            }
        }else{
            return FALSE;
        }
    }

我添加了我的观点

<?php foreach ($details as $s) { ?>
    <div class="col-md-5">
        <div class="box text-center">
            <img src="<?php echo base_url('upload/'.$s->photo); ?>" width="150px" height="150px">
            <br><?php echo $s->productName; ?>
            <br><strong>Rp. <?php echo $s->price; ?></strong>
            <br>
            <?php echo form_open('member/add'); ?>
            <fieldset>
                <label>Quantity</label>
                <?php echo form_input('quantity', '1', 'maxlength="2"'); ?>
                <?php echo form_hidden('product_id', $s->productID); ?>
                <?php echo form_submit('add', 'Add'); ?>
            </fieldset>
            <?php echo form_close(); ?>
        </div>
    </div>
<?php } ?>

Jquery脚本

 <script type="text/javascript">
    $(document).ready(function() { 
        /*place jQuery actions here*/ 
        var link = "<?php echo site_url('member/detail')?>/"; // Url to your application (including index.php/)

        $(".detail-product").submit(function(){
            var id = $(this).find('input[name=product_id]').val();
            var qty = $(this).find('input[name=quantity]').val();

            $.post(link + "member/add", { product_id: id, quantity: qty, ajax: '1' },
                function(data){ 
                    if(data == 'true'){

                        $.get(link + "member/detail", function(cart){ // Get the contents of the url cart/show_cart
                            $("#cart_content").html(cart); // Replace the information in the div #cart_content with the retrieved data
                        });          

                    }else{
                        alert("Product does not exist");
             });

            return false; // Stop the browser of loading the page defined 
        });

    });
    </script>

这是问题网址:http://localhost/scientificwriting/member/detail/并且无法调用productid。我是否需要替换控制器上的IF语句和我的jquery?

请帮助我谢谢

0 个答案:

没有答案