How to redirect to another view after sending email?

Asked

Viewed 161 times

1

In the code below, after sending the form via e-mail, the same is loaded view. Instead, I need you to be directed to another view. (thanks for example)

Below follows the code:

public function duvidas_e_contatos() {
    $this->load->helper('form');
    $this->load->library('form_validation');
    // REGRA DE VALIDAÇÃO
    $this->form_validation->set_rules('firstname', 'Nome', 'trim|required');
    $this->form_validation->set_rules('lastname', 'Sobrenome', 'trim|required');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('mobilephone', 'Celular', 'trim|required');
    $this->form_validation->set_rules('country', 'País', 'trim|required');
    $this->form_validation->set_rules('subject', 'Motivo do Contato', 'trim|required');
    $this->form_validation->set_rules('coment', 'Mensagem', 'trim|required');
    $this->form_validation->set_error_delimiters('<small class="text-danger">', '</small>');

    $infoPage['form_error']     = validation_errors();
        $infoPage['title']          = '';
        $infoPage['description']    = '';
        $infoPage['keywords']       = '';
        $infoPage['canonical']      = '';
        $infoPage['autor']          = '';
        $infoPage['webmaster']      = '';
        $infoPage['credits']        = ''

    if ($this->form_validation->run() == FALSE) {
        $infoPage['form_error']     = validation_errors();
        $infoPage['title']          = '';
        $infoPage['description']    = '';
        $infoPage['keywords']       = '';
        $infoPage['canonical']      = '';
        $infoPage['autor']          = '';
        $infoPage['webmaster']      = '';
        $infoPage['credits']        = '';

        $this->load->view('duvidas_e_contatos', $infoPage);

    } else {
        $formrequest = $this->input->post();
        date_default_timezone_set('America/Sao_Paulo');
        $this->load->library('email');
        $this->email->set_newline("\r\n");
        $this->email->initialize();

        $formrequest = $this->input->post();

        $subject = $formrequest['subject'];

        $mensagem_firstname     = $formrequest['firstname'];
        $mensagem_lastname      = $formrequest['lastname'];
        $mensagem_email         = $formrequest['email'];
        $mensagem_mobilephone   = $formrequest['mobilephone'];
        $mensagem_country       = $formrequest['country'];
        $mensagem_subject       = $formrequest['subject'];
        $mensagem_coment        = nl2br($formrequest['coment']);

        $message = "html";

        $body = $message;

        $result = $this->email
                ->from('')
                ->reply_to($formrequest['email'], $formrequest['firstname'])
                ->to('')
                ->cc('')
                ->bcc($formrequest['email'])
                ->subject($subject)
                ->message($body)
                ->send();

        $infoPage['firstname']      = $formrequest['firstname'];
        $infoPage['lastname']       = $formrequest['lastname'];
        $infoPage['email']          = $formrequest['email'];
        $infoPage['mobilephone']    = $formrequest['mobilephone'];
        $infoPage['country']        = $formrequest['country'];
        $infoPage['subject']        = $formrequest['subject'];
        $infoPage['coment']         = $formrequest['coment'];

        $infoPage['title']          = 'Confirmação de Contato';
        $infoPage['description']    = '';
        $infoPage['keywords']       = '';
        $infoPage['canonical']      = '';
        $infoPage['subtitle']       = 'Sua mensagem foi recebida!';
        $infoPage['autor']          = '';
        $infoPage['webmaster']      = '';
        $infoPage['credits']        = '';

        $this->load->view('mensagem_enviada', $infoPage);
    }
}

1 answer

3


A legal way would be to change where it calls:

$this->load->view('mensagem_enviada', $infoPage);

And use in this way:

if ($result)
{
     redirect('/aondequiser', 'refresh');
}

Remembering that for this you will need the helper url charged:

$this->load->helper('url');
  • Thank you very much @Otto turned out to be exactly how I solved this problem. I ended up finding redirect in the documentation -> [link]https://codeigniter.com/user_guide/helpers/url_helper.html?highlight=redirect#redirect[link].

  • @Williansilva if it helped you mark as solved the question.

Browser other questions tagged

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