Flash data
Last updated: Dec 04, 2023

Introduction

Flash data is session data that's only present on the first page visit after it has been set. After that first pagevisit, the session is destroyed. This can be very handy for display messages such as confirmation messages after form submits, or validation error messages. You can have as many flash entries as you like.

Setting flash data

You can use the setFlashdata() method to create flash data. This method belongs to the Webigniter class, so if you want to set some flash data in your custom form logic for example, you can use it like this.

    public function contact_form(): bool
    {
        if($this->formData['name'] != "harry")
        {
            $this->webigniter->setFlashdata('error', 'You are not Harry!']);
            return false;
        }

        return true;
    }

This will add a flash entry with the name 'error' and value 'You are not Harry!' after the contact_form is submitted and the name is not Harry.

Getting flash data

After you have set flash data, you need to be able to show it in your view files obviously.

Please remember that flash data will only be present the first page visit after it has been set, a refresh for example would make it blank again.

This is how you can get flash data in your views.

<?php
if($webigniter->getFlashData('error')):?>
   <div class='error'>
      <?=$webigniter->getFlashData('error');?>
   </div>
<?php endif;?>

This piece of code first checks if there is an error, and if so, show it.

1