CodeIgniter Forums

Full Version: [resolved] Using flashdata on the same page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]FinalFrag[/eluser]
Let me start of with a snippet of my view:

Code:
<? if ($this->session->flashdata('message') != '') { ?>
    <div id="feedback" class="&lt;?=$this->session->flashdata('class')?&gt;">
        <p>&lt;?=$this->session->flashdata('message')?&gt;</p>
    </div>
&lt;? } ?&gt;

What that does is pretty easy. When the 'message' is set in flashdata, it creates a div to show that message.

What I need is this message to show on the first page the visitor visits. So I've made my home controller (the first one the visitor sees when getting to my site). It looks like this:

Code:
class Home extends Controller {
    function index() {
        // Generate feedback
        $this->session->set_flashdata('class', 'notice');
        $this->session->set_flashdata('message', 'This website is a <abbr title="Work In Progress">WIP</abbr>.');
        
        redirect('home/showme', 'location');
    }
    
    function showme() {
        $data['header_pagetitle'] = 'Home';
        $data['header_menu'] = ul(array(anchor('', 'Home', array('id' => 'current-tab')), anchor('about', 'About me'), anchor('portfolio', 'Portfolio'), anchor('contact', 'Contact'), anchor('agenda', '_Agenda'), anchor('photos', '_Photos')));
        $data['header_title'] = 'Home';
        //$data['header_javascript'] = '';
        
        $this->load->view('home', $data);
    }
}

What this SHOULD do:
- generate the flashdata and save it in the session
- redirect to mysite.com/home/show
- create all the needed variables in the $data array
- show the home view

It does all that but there's just 1 problem. It shows the home view without a stylesheet Confused
Although, when I take a look at the source of that page it has included my style.css file.

When I try to change the redirect line to: redirect('otherpage', 'location'); it works. So I'm assuming this happens because I redirect to a function in the same controller. Could this be true?




Now you might say: 'just use $this->showme'. I could, but then the flashdata would not be shown on the view (because flashdata only shows on the next request).

Anyone got something on this issue?

El Forum

[eluser]FinalFrag[/eluser]
Ok, I figured out why it didn't work. I had this in the head section of my page:

Code:
&lt;link rel="stylesheet" type="text/css" href="style.css" media="all" /&gt;

And I should be this:

Code:
&lt;link rel="stylesheet" type="text/css" href="&lt;?=base_url()?&gt;style.css" media="all" /&gt;

That's why the stylesheet didn't work, nor my javascript (but I hadn't noticed that before).

Sometimes you just want to slap yourself when you finaly find what was wrong. This is one of these times Smile

El Forum

[eluser]neen[/eluser]
Flashdata is available on the next request only (unless you set save_flashdata on the next request)

It works because you have redirected (thus causing another browser request)

What you can do is keep track of the url the user came from, and redirect them back to that url, after setting the flashdata. This will allow the flashdata to display, and should not affect your CSS or anything else.

Haha...Good 'ol PEBKAC

Glad you got it figured out