简体   繁体   中英

Codeigniter - Page redirect not working after submitting button

I'm trying to insert and update data in my database using ajax to my controller. Now my data is inserting and updating precisely after I click the button. But my data on my view page is not updating. I need to refresh again the page to update my view and the success message to display

PS: It's firing sometimes, and sometimes not, Can't understand the behaviour

Below is my JS file for ajax

$('#triger').click(function(){
                    var btn_value = $('#triger').val();
                    var tenant_id = $('#tenant_id').val();
                    var calldisp_id = $('#calldisp_id').val();
                    var disposition_name = $('#disposition_name').val();
                    var disposition_code = $('#disposition_code').val();
                    var email = $.map($("#tags span"), function(elem, index){
                         return $(elem).text();
                    }); 
                    var myJsonString = JSON.stringify(email);
                    //alert(myJsonString);
                        if(btn_value == 'Create'){

                            $.ajax({
                                url:"<?php echo base_url();?>admin/call_disposition/create_email_dispo_dre",
                                method:"POST",
                                data:{email:myJsonString,
                                      disposition_name:disposition_name,
                                      disposition_code:disposition_code,
                                      tenant_id:tenant_id},
                                dataType: 'json',
                                success:function(data){

                                },
                            });
                        }
                        else if(btn_value == 'Update'){
                            $.ajax({
                                url:"<?php echo base_url();?>admin/call_disposition/update_email_dispo_dre",
                                method:"POST",
                                data:{email:myJsonString,
                                      disposition_name:disposition_name,
                                      disposition_code:disposition_code,
                                      calldisp_id:calldisp_id,  
                                      tenant_id:tenant_id},
                                dataType: 'json',
                                success:function(data){


                                },
                          });
                        }

                });

Below is my Controller

public function create_email_dispo_dre($id){

    $this->_rules();

    if ($this->form_validation->run() == FALSE) {
        $this->update($id);
    } else {
    $data = array(
            'tenant_id' => $this->input->post('tenant_id',TRUE),
            'disposition_code' => $this->input->post('disposition_code',TRUE),
            'disposition_name' => $this->input->post('disposition_name',TRUE),
            'email' => $this->input->post('email',TRUE)           
            );
     $this->calldisp_model->insert($data);
     $this->session->set_flashdata('message', 'admin_faqs_success');
     redirect('admin/call_disposition/update/'.$id);

    }

}

In the ajax request for update: In the success section:

success:function(data){
},

you need to call a page reload:

window.location.reload();

so your code becomes:

success:function(data){
    window.location.reload();
},

In controller

you have to add

echo json_encode(array('success'=>'1')); instead of redirect('admin/call_disposition/update/'.$id); because you used datatype "json";

and change success function an ajax request Like

success:function(data){
   if(data.success=='1'){
     window.location.reload();
   }
 }

In codeigniter when you execute an Ajax call you can't redirect from the controller function. If you want to seemlessly update the page after clicking #trigger you have to echo the result in your controller

echo json_encode($html_you_want_to_display);

and then in your ajax success clause you need to update the div with the result from the echo by setting the innerHtml to the result. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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