19

I'm struggling with Flash Data in CodeIgniter.

I basically want to:

add a category to a database redirect user back to a page show a success pop-up message "Your category has been created"

So far I can add the category successfully to the db and the user input is validated correctly, only thing is I don't know how to create the pop up success message. (I don't want to load a success view), just redirect back to where they came from and show small message in the top corner or something.

Is flash data the right way to go?

7 Answers 7

44

In your controller:

//add to db
// load session library if not auto-loaded
$this->session->set_flashdata('msg', 'Category added');
redirect('controller/method');

In the view:

<script>
// assumes you're using jQuery
$(document).ready(function() {
$('.confirm-div').hide();
<?php if($this->session->flashdata('msg')){ ?>
$('.confirm-div').html('<?php echo $this->session->flashdata('msg'); ?>').show();
<?php } ?>
});
</script>
3
  • Cheers Dude! Now I can neaten this up with css and more javascript effects, Thanks!
    – Tom
    Sep 10, 2012 at 14:30
  • 1
    Just noticed something that may want an adjustment. The closing brace for the php call needs to be in the document ready function. Otherwise it may give errors. $(document).ready(function() { $('.confirm-div').hide(); <?php if($this->session->flashdata('msg')){ ?> $('.confirm-div').html('<?php echo $this->session->flashdata('msg'); ?>').show(); <?php } ?> });
    – P070
    Dec 2, 2018 at 11:05
  • @Le-Nerdtm you are correct, I have adjusted my answer above.
    – Mudshark
    Dec 5, 2018 at 12:39
6

Your can perform different session message depends what you pass to view from your controller. Noted that I am using Bootstrap as my CSS backbone.

In view,

For success case,

<?php if ($this->session->flashdata('category_success')) { ?>
        <div class="alert alert-success"> <?= $this->session->flashdata('category_success') ?> </div>
    <?php } ?>

For error case,

<?php if ($this->session->flashdata('category_error')) { ?>
    <div class="alert alert-danger"> <?= $this->session->flashdata('category_error') ?> </div>
<?php } ?>

In controller,

For success case,

$this->session->set_flashdata('category_success', 'Success message.');
redirect("To your view");

For error case,

$this->session->set_flashdata('category_error', 'Error message.');
redirect("To your view");

For more reference you can visit: http://www.codeigniter.com/userguide2/libraries/sessions.html

1

using ternary operator :

Setting Flash Data:

$this->session->set_flashdata('insertproduct', 'Product added successfully');
$this->session->set_flashdata('deleteproduct','Delete added successfully');

Using the Flash Session Data:

<?php if($this->session->flashdata('insertproduct')):echo $this->session->flashdata('insert');endif; ?><br/>
<?php if($this->session->flashdata('delete')): echo $this->session->flashdata('delete'); endif;?>
1

You can try this -

Controller:

    $this->session->set_flashdata('success', 'Success Message...');

    OR

    $this->session->set_flashdata('error', 'Error Message...');

    OR

    $this->session->set_flashdata('warning', 'Warning Message...');

    OR

    $this->session->set_flashdata('info', 'Info Message...');

View:

    <?php if($this->session->flashdata('success')){ ?>
        <div class="alert alert-success">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Success!</strong> <?php echo $this->session->flashdata('success'); ?>
        </div>

    <?php } else if($this->session->flashdata('error')){  ?>

        <div class="alert alert-danger">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Error!</strong> <?php echo $this->session->flashdata('error'); ?>
        </div>

    <?php } else if($this->session->flashdata('warning')){  ?>

        <div class="alert alert-warning">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Warning!</strong> <?php echo $this->session->flashdata('warning'); ?>
        </div>

    <?php } else if($this->session->flashdata('info')){  ?>

        <div class="alert alert-info">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Info!</strong> <?php echo $this->session->flashdata('info'); ?>
        </div>
    <?php } ?>
0

Yes, just check if the flash data is available, if it is, show the message, if it isn't, then don't show it. as simple as that.

p.s. you should always do a redirect after a POST request.

2
  • Could you explain why you should always redirect after a POST request ? Is it to unset session data ?
    – Fifi
    Oct 3, 2018 at 10:12
  • Its a common design pattern to prevent duplicate form submission: en.wikipedia.org/wiki/Post/Redirect/Get
    – hndr
    Oct 4, 2018 at 14:48
0

CodeIgniter's Flash data utilizes PHP session variables. It places a :old in the session name so that it only lasts for one db call. It's very function and purpose is to do what you are wanting to do, so, yes, it's a very good way of going about these types of things.

Remember if you are going to use this you must include $this->session->library('session')

If you are not sure how to actually use flash_data than I would suggest reading the docs I previously linked.

$this->session->set_flashdata(
    'category_success', 
    'Your category has been created'
);
redirect(); //location

echo $this->session->flashdata('category_success');
0
//Set Flash messages

$this->session->set_flashdata('post_created', 'Your post has been Posted!');
redirect('Posts/index');

//In Posts View you will have

<?php if($this->session->flashdata('post_created')) : ?>
    <?php echo '<p class="alert alert-success"> ' .$this->session->flashdata('post_created'). '</p>'; ?>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.