简体   繁体   English

如何使用JavaScript for Codeigniter在会话中删除项目?

[英]How to remove the item in session using javascript for codeigniter?

I am working on a shopping cart php(codeigniter) project. 我正在从事购物车php(codeigniter)项目。 So I have add the item in the session like the code following. 因此,我已在会话中添加了该项目,例如以下代码。 The problem is, I would like to remove the item in session in the checkout page. 问题是,我想在结帐页面的会话中删除该项目。

But of course I can not call the php function to remove the session in javascript , that means , when the remove button is click , how can I do (not restrict to use ajax, simple is the best), I can remove the item in session ? 但是,当然我不能调用php函数来删除javascript中的会话,这意味着,当单击“删除”按钮时,我该怎么办(不限制使用ajax,最简单的方法就是最好),我可以在会议? Thanks 谢谢

   if ($this->form_validation->run()) {
                    if ($this->session->userdata('purchase') !== false)
                        $purchase_list = $this->session->userdata('purchase');
                    else
                        $purchase_list = array();

                    $purchase = array(
                        'id' => $product[0]['id'],
                        'quantity' => $this->input->post('quantity')
                    );

                    if ($this->input->post('opt1') !== false)
                        $purchase['opt1'] = $this->input->post('opt1');

                    if ($this->input->post('opt2') !== false)
                        $purchase['opt2'] = $this->input->post('opt2');

                    array_push($purchase_list, $purchase);
                    $this->session->set_userdata('purchase', $purchase_list);
                    redirect('cart');
                }

from CodeIgniter's session class documentation 从CodeIgniter的会话类文档中

This assumes you understand your cart and will know where to actually unset session data in your checkout scenery. 假设您了解您的购物车,并且知道在结帐环境中实际取消会话数据的位置。

you can set the value of desired item in your session using this syntax: 您可以使用以下语法在会话中设置所需项目的值:

$this->session->set_userdata('purchase', null); // will set the purchase session data to null

you only know which key to set to null though 你只知道哪个键设置为空

You can define a function in your controller to unset the session eg 您可以在控制器中定义一个函数以取消会话设置,例如

    class Controllername extends CI_Controller {

    function index(){}

    function reset_session(){
$sesion_variable = 'name_of_session';
    $this->session->unset_userdata($session_variable);
    }

    }

And you can call that via CURL or ajax. 您可以通过CURL或ajax调用它。 On checkout page 在结帐页面上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM