How to pass variable between views?

Asked

Viewed 199 times

0

My problem is this. My project’s Dashboard has an input where the user inserts the process number and clicks to search, as code below:

  <form class="navbar-form navbar-left" role="search" action="<?= base_url() ?>processo/pesquisar" method="post">
        <div class="form-group">
            <input type="text" style="width: 250px;" name="pesquisar" class="form-control" placeholder="Digite o número do processo" required>
            <button type="submit" class="btn btn-default btn-md"> CONSULTAR</button>
        </div>            
    </form>

When you click search, I call the search method of the Process controller, as below, which calls the get_processos_like method of the model, Processo_model:

public function pesquisar() {
    $this->load->model('Processo_model', 'processo');
    $dados['processo'] = $this->processo->get_processos_like();
    if (!$this->processo->get_processos_like()) {
        $data["msg"] = "Processo não encontrado.";
        $this->load->view('includes/msg_erro', $data);
    } else {
        $this->load->view('/listar/listar_processo_adv', $dados);
    }         
}

 #Método get_processos_like do Model:

 function get_processos_like() {
    $termo = $this->input->post('pesquisar');
    $this->db->select('*');
    $this->db->where('nprocesso', $termo);
    return $this->db->get('processo')->result();
}

This method returns the data from the searched process to view listr_processos_adv.php, which displays all process information and has a link calling another view, as below:

    <div class="row">     
                <div align="center">
                    <button type="button" title="Detalhes" onclick="window.location.href = '<?= base_url('processo/custas')?>'" class="btn btn-primary>INFORMAÇÕES DE CUSTAS</button>
                </div>
    </div>  

Until then everything works, it happens that I need to pass the number of this process found and displayed to another view, called listr_processo_custas.php, which displays in addition to some information about the process, displaying its costs (expenses) of the process. Well my dear, I turned around a lot, but the question is simple: How do I pass a variable to another view in Codeigniter?

thank you in advance!

1 answer

2


You can pass the value via GET to the URL for the method custas. I suggest using <a> in place of <button>. You can use class="btn btn-primary" in the <a> also. It would basically look like this:

<div class="row">     
    <div align="center">
        <a href="<?=base_url('processo/custas/'.$codProcesso)?>" target="_blank" class="btn btn-primary">INFORMAÇÕES DE CUSTA</a>
    </div>
</div>

Remember that in Codeigniter the variables that are passed via GET can be footprints from the parameters of the controller method. In your case. the method custas of the controller processo it must be so: public function custas($codProcesso) {...} and within this method you will find in the database the details of the process and send to the details page you develop. ($this->load->view(...))

I hope I’ve helped.

  • Simple like that, thank you very much my dear @Paulo Weverton

  • Would it be possible to pass the codProcesses in a hidden way? Because it is at the url.

Browser other questions tagged

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