Welcome Guest, Not a member yet? Register   Sign In
Strange behaviour using set_flashdata and redirect

I have a controller function called add
This function has a main if, that checks if something was posted from the view's form.

if ($this->input->post('field1'))
   ...load model and insert data
   $this->session->set_flashdata('msg', 'Data added!!');
   ...Load models, helpers, etc.
   ...Load view with form

All goes fine, when I submit the form, data is added correctly, but the view doesn't load correctly. I mean, the models, helpers, etc. seem not to load when the else block code is called from redirect(...)

Hope you've understood me


Based on this it's pretty much impossible to see what causes the problem. I'm kinda sceptical that it might be the flashdata actually. You've edited out so much wherein a problem might lie and the parts you have posted seem al right.

Also, what kind of output do you get? Do you get an error or what happens from which you conclude the resources weren't loaded?

[eluser]Ben Edmunds[/eluser]
So you're redirecting back to the same method???

[quote author="Ben Edmunds" date="1271211099"]So you're redirecting back to the same method???[/quote]

Basically, why? Because I want user to continue adding data.
I mean, I want user to avoid being redirected to a "Thank you page", and then to click back to go to the same controller for adding a new record.

I'm doing these controllers for data-entry users

[eluser]Ben Edmunds[/eluser]
Just seems like an odd flow...

So post more of your code and we'll go from there.

I've had some very bizarre experiences using CI's Flash Session Data. For instance, I had a javascript resource being called in the head area of my html, and it was pointing to a resource that didn't actually exist, and that was causing the flashdata to disappear. Once I removed the call to the javascript, the flashdata worked fine. I imagine that the 404 error created by the resource that didn't actually exist was using up my stored flashdata... but I'm just guessing.

In your case I think it might be worth considering that CI's flashdata is stored in a cookie, and isn't a true $_SESSION array on the server. We all know that a true $_SESSION is stored on the server, but cookies are stored on the site visitor's browser. Because the controller is never serving up a view, but rather redirecting, then the cookie is never set. You could probably verify this by using an HTTP header tool to look at what is going on.

Here is the complete controller's function code

function agregar ()
    $data = array();

    if ($this->input->post('campopost')) {
        $this->load->model( array('tag_abm', 'articulo_abm') );

        /* Datos propios del Articulo */
        $campotitulo = $this->input->xss_clean($this->input->post('campotitulo'));
        $campodescripcion = $this->input->xss_clean($this->input->post('campodescripcion'));
        $campoautor = $this->input->xss_clean($this->input->post('campoautor'));
        $campofechaactualizacion = $this->input->xss_clean($this->input->post('campofechaactualizacion'));
        $campofechavencimiento = $this->input->xss_clean($this->input->post('campofechavencimiento'));
        $campocategoria = $this->input->xss_clean($this->input->post('campocategoria'));
        $postdata = array (
            'campotitulo' => $campotitulo,
            'campodescripcion' => $campodescripcion,
            'campoautor' => $campoautor,
            'campofechaactualizacion' => $this->utilidades->fecha_mysql($campofechaactualizacion),
            'campofechavencimiento' => $this->utilidades->fecha_mysql($campofechavencimiento),
            'campocategoria' => $campocategoria
        $idArticulo = $this->articulo_abm->agregar($postdata);

        /* Added, now to redirect with refresh so flashdata works */
        $this->session->set_flashdata('mensaje', 'Los datos del articulo han sido agregados exitosamente');
        /* The first 3 lines seem not to be executed when coming back from redirect */
        $data['resultados_categorias'] = $this->categoria_abm->obtener_categorias();

        $data['dir_views'] = $this->config->item('dir_views');
        $data['titulo_pagina'] = "Artículos - Agregar";
        $this->load->view('vista_articulo_agregar', $data);    

Maybe I'm getting it wrong but as far as I understand it you're loading an articles-model in the if-part and a categories-model in the else-part while loading a view that's called after articles. Might that be the problem?

You might want to take a look at my post ^ if you missed it.

To be honest @skunkbad, didn't get any idea from your post. My english su**s

Theme © iAndrew 2016 - Forum software by © MyBB