CodeIgniter Flash Data

2019-02-08 16:21发布

问题:

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?

回答1:

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>


回答2:

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



回答3:

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;?>


回答4:

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 } ?>


回答5:

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.



回答6:

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');


回答7:

//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; ?>