CodeIgniter Forums
Function Redirect() not send to browser - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Function Redirect() not send to browser (/showthread.php?tid=78309)



Function Redirect() not send to browser - mabraham - 12-31-2020

I try redirect from a controller to another controller, and the url in the browser is not updated, even though the driver is loaded.
Performing inspection of the code, I observe that the load returns http 200 code, but does not refresh the screen.

View.php
Code:
    function AdministrarOMejoras(id)
    {
        // Redireccion al controller de Oportunidades
         $.post(
            '<?php echo site_url('C_Evaluacion/ajax_AdministrarOMejoras')?>',
                {
                IdEvaluacion: id
                }
        );
    }   


Controller C_Evaluacion.php

Code:
    public function ajax_AdministrarOMejoras()
    {
        $this->load->helper('url');
        $IdEvaluacion = $this->input->post('IdEvaluacion');

        redirect('Mejoras/'.$IdEvaluacion, 'location' );

    }  
   

Route

Code:
$route['Mejoras/(:any)'] = 'C_OportunidadMejora/getOMByEvaluation/$1';



Mejoras/getOMByEvaluation Controller/Method
Code:
    function getOMByEvaluation($idEv){  
       
         $data["IdEvaluacion"] = $idEv;
         $data["title"] = "Oportunidades de Mejora"; 
        
         $this->load->view('layouts/Header', $data);        
         $this->load->view('operaciones/OportunidadesMejora_view', $data); 
         $this->load->view('layouts/Footer');         

    } 


Code inspection




OportunidadesMejora_view never show in browser.


can you help me with this?
Appreciate your help
Tnxs


RE: Function Redirect() not send to browser - kleber - 01-17-2021

Your request is correct. However, you made a request via ajax. In other words, you remain on the page while the request is made behind the scenes.
One of the benefits of this type of request is that it does not interfere with the display of your page.
For example on like buttons where this is commonly used, you would not want your user to be redirected to another page when clicking on a link / button. This requisition is made under page.
To achieve what you want it is necessary to add a redirect via javascript after this POST.

     
Code:
// Redireccion al controller de Oportunidades
        $.post(
            '<?php echo site_url('C_Evaluacion/ajax_AdministrarOMejoras')?>',
                {
                IdEvaluacion: id
                },function(data){
            window.location = '<?php echo site_url('Mejoras/')?>'+data;
});


The data returned from the controller will be the $IdEvaluacion.

remove this from the controller:

Code:
redirect('Mejoras/'.$IdEvaluacion, 'location' );


and add:

Code:
echo $IdEvaluacion;