使用 ajax 添加了一条新记录并且 codeigniter.i 想在添加记录后重定向到查看页面。所以我想获得最后的插入 ID

added a new record using ajax and codeigniter.i want to redirect to the view page after adding record. so i want to get the last insert id

Ajax 函数和控制器动作给出 below.anyone 可以帮助获得最后的 insertid 到 redirect.the 重定向在 ajax 成功函数中完成。它可以在控制器中做 action.its 没有得到

Ajax 函数

<script type="text/javascript">

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

        //var title = $('#title').val();

        var form_data = {
            title: $('#txtname').val(),
            merid: $('#mermerchant').val(),
            userid: $('#customer').val(),
            description: $('#txtrequirement').val(),
            reqid: $('#requirementid').val(),
            shipmethod: $('#shipmethod').val(),
            shiplocation: $('#shiplocation').val(),
            attachment: $('#txtattachments').val(),
            bidclose: $('#txtbidclose').val(),
            ajax: '1'
        };

        $.ajax({
            url: "<?php echo base_url() ?>moderator/RFQ/addoffline",
            type: 'POST',
            data: form_data,
            success: function () {

                 window.location.href ="<?php  echo base_url() ?>moderator/Employee/manageemployee/";
                // window.location.href ="<?php //echo base_url()             ?>moderator/RFQ/viewrfq/"+  form_data.reqid;
//      alert('added Successfully');
            }
        });

        return false;
    });

</script>

控制器动作

     $this->load->helper(array('form', 'url'));
           $this->load->helper('file');
        $data7 = array(
            'rfq_title' => $this->input->post('title'),
            'rfq_detail' => $this->input->post('description'),
            'rfq_merchantid' => $this->input->post('merid'),
            'rfq_userid' => $this->input->post('userid'),

        );
       $add= $this->requirement_model->forminsert($data7);
}

你的问题无论如何都不清楚,如果我有你需要的,试试这个。

in your controller action return json response:

   $this->load->helper(array('form', 'url'));
             $this->load->helper('file');
             $data7 = array(
                'rfq_title' => $this->input->post('title'),
                'rfq_detail' => $this->input->post('description'),
                'rfq_merchantid' => $this->input->post('merid'),
                'rfq_userid' => $this->input->post('userid'),

            );
           $inserted_id= $this->requirement_model->forminsert($data7);
           //your forminsert method in model should return 
           //$this->db->insert_id();
           $response=array('id'=>$inserted_id,'message'=>"inserted successfully"); 
           echo json_encode($response); 
           die();
    }

Your model function return last_inserted_id:

public function forminsert($data7) 
{ 
$this->db->insert('jil_mrorfq',$data7); 
//it will return last id
return $this->db->insert_id();
} 

Now in ajax success :

   $.ajax({
         url: "<?php echo base_url() ?>moderator/RFQ/addoffline",
         type: 'POST',
         data: form_data,
         dataType:"Json",
         success: function (data) {
         //to access that id
         var last_inserted_id = data.id;
         window.location.href ="<?php  echo base_url() ?>moderator/Employee/manageemployee/"+last_inserted_id;

         }
        });