如果插入是由AJAX完成的,如何在codeigniter会话中存储最后一个插入ID?

时间:2014-07-07 10:32:20

标签: php jquery mysql ajax codeigniter

我需要在某个事件中插入用户的最后一个插入ID ..... 我希望在另一页上得到它.... 喜欢在产品页面....用户自定义他的产品......如mug customization

我需要将插入的....自定义产品ID添加到购物车页面,这是由ajax ....所以如何在产品页面的购物车页面上获取最后一个插入ID .... / p>

我的ajax ......

$('#store').click(function () {

    $.ajax({
        type: "POST",
        url: ajax_url_store,
        data: {
            action: 'store',
            views: JSON.stringify(thsirtDesigner.getProduct())
        },

        success: function (data) {

            if (parseInt(data) > 0) {
                //successfully added

                document.getElementById('succes-message').innerHTML = 'You Design has Been SuccessFully Saved';
            } else {

            }

        },
        error: function () {
            //alert('some error has occured...');
        },
        start: function () {
            //alert('ajax has been started...');    
        }
    });
});

我的ci功能

public function saveData() {

    if ($this - > input - > post('action') == 'store') {

        $views = $this - > input - > post('views');
        $id = $this - > product_model - > save($views);

        if ($id != '') {
            header('Content-Type: application/json');
            echo json_encode($id);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

public function saveData() 
{

  if ($this->input->post('action') == 'store') 
  {

    $views = $this->input->post('views');
    $id = $this->product_model->save($views);
    $data = array('userid'=>$id);       //insert id to userid as session variable
    $this->session->set_userdata($data);  //set userid as a session variable

    if ($id != '') 
    {
        header('Content-Type: application/json');
        echo json_encode($id);
    }
  }
}