Search code examples
ajaxcodeignitercodeigniter-2codeigniter-3

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 function and controller action is given below.anyone can help to get the last insertid to redirect.the redirection is done in ajax success function. can it do in controller action.its not getting

Ajax function

<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>

Controller action

     $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);
}

Solution

  • Your question is not clear anyhow if i got what you need try this.

    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;
    
             }
            });