Redirect after executing codeigniter function

Asked

Viewed 642 times

0

Below I show the code that I use to call a function after performing the function I use a redirect to the home page, however I want to give a modic to this because if the user uses some filter when performing this function he will lose the filters, generating the work of locating the records every time, so I want to know if there is any way to direct the user to the previous page with your filters?

Function:

function inserir_coment(){
    $this->template->set('title', 'Novo Comentario');
    /* Carrega a biblioteca do CodeIgniter responsável pela validação dos 
    formulários */
    $this->load->library('form_validation');

    /* Define as tags onde a mensagem de erro será exibida na página */
    $this->form_validation->set_error_delimiters('<span>', '</span>');

    /* Define as regras para validação */
    $this->form_validation->set_rules('comentdate', 'Data', 'required');
    $this->form_validation->set_rules('nomeC', 'Cliente', 'required');
    $this->form_validation->set_rules('texto', 'Texto', 'required');
    $this->form_validation->set_rules('autor', 'Autor', 'required');


    /* Executa a validação e caso houver erro chama a função index do controlador */
    if ($this->form_validation->run() === FALSE) {
        $this->template->load('layout', 'inserir_coment.phtml');
    /* Senão, caso sucesso: */
    } else {
        /* Recebe os dados do formulário (visão) */
        $data['comentdate'] = $this->input->post('comentdate');
        $data['cliente'] = $this->input->post('nomeC'); 
        $data['texto'] = $this->input->post('texto');
        $data['autor'] = $this->input->post('autor');


        /* Chama a função inserir do modelo */
        if ($this->model->inserir_coment($data)) {
            $this->session->set_flashdata('mensagem', "<div class='alert alert-success'> Comentario inserido com sucesso</div>");
            redirect('comments');
        } else {
            $this->session->set_flashdata('mensagem', "<div class='alert alert-danger'> Erro ao inserir comentario</div>");
        }
    }
}
  • If filters are parameter GET, try to use this way redirect($this->agent->referrer());. https://www.codeigniter.com/userguide3/libraries/user_agent.html

1 answer

0

Echo this function Javascript can work

     echo   "window.history.go(-1);";

It will go back to the previous page keeping the form data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.