Redirect of codelgniter

1
I'm trying to put this alert with a Codelgniter function that is% with%. I put it to redirect the page, but alert does not work.

What can it be?

$data['categoria'] = $this->input->post('categoria');
$data['slug_categoria'] = $this->input->post('slug_categoria');
$this->db->insert('categorias', $data);
redirect('administracao/categorias');
echo "<script>alert('Inseridos!')</script>";

How do I get this alert message to work with redirect? that way and another one I've tried does not work.

    
asked by anonymous 22.06.2014 / 09:06

1 answer

1

Explanation:

1)

2) The redirect > which is a CodeIgniter helper , is to redirect to the specified address, and internally would be header("Location: /administracao/categorias"); so it will neither run the last line which is a Javascript code (follow recommendation item 1). See the code below:

function redirect($uri = '', $method = 'location', $http_response_code = 302)
{
    if ( ! preg_match('#^https?://#i', $uri))
    {
        $uri = site_url($uri);
    }
    switch($method)
    {
        case 'refresh': 
            header("Refresh:0;url=".$uri);
            break;
        default: 
            header("Location: ".$uri, TRUE, $http_response_code);
            break;
    }
    exit;
}

3) Solution:

In your method before redirect , use a $this->session with the method flashdata , which is meant to be active only until the next server request and immediately after it is automatically deleted.

Method:

$data['categoria'] = $this->input->post('categoria');
$data['slug_categoria'] = $this->input->post('slug_categoria');
$this->db->insert('categorias', $data);
$this->session->set_flashdata('acaoform', 'Inseridos !!!.');
redirect('administracao/categorias');

View

<?php
    $acaoflash = $this->session->flashdata('acaoform');    
    if (isset($acaoflash) && $acaoflash!=''){
        echo "<script>alert('".$acaoflash."')</script>";
    }
?>

References:

22.06.2014 / 16:50