phpcodeignitermodel-view-controllerflash-messagecodeigniter-query-builder

Codeigniter setflash data wont clear after refresh


enter image description here

hi everyone who had an idea on how to clear the setflashdata? im just new at codeigniter Framework.

This is my Controller view that has the submit function and view for the page add view

Controller:-

 public function add(){
            $this->load->view('layout/header');
            $this->load->view('blog/add');
            $this->load->view('layout/footer');
        }
    
        public function submit(){
            $result = $this->m->submit();
            if($result){
                $this->session->set_flashdata('success_msg', 'Record added successfully');
            }else{
                $this->session->set_flashdata('error_msg', 'Faill to add record');
            }
            redirect(base_url('blog/index'));
        }

This is the view html page for the datable and add record button.

 <h3>Blog list</h3>
    
        <?php
            if($this->session->flashdata('success_msg')){
        ?>
            <div class="alert alert-success alert-status">
                <?php echo $this->session->flashdata('success_msg'); ?>
            </div>
        <?php       
            }
        ?>
    
    
        <?php
            if($this->session->flashdata('error_msg')){
        ?>
            <div class="alert alert-success">
                <?php echo $this->session->flashdata('error_msg'); ?>
            </div>
        <?php       
            }
        ?>

//this is the button that has the add function.

<a href="<?php echo base_url('blog/add'); ?>" class="btn btn-primary">Add New</a>

This is the datatable view

<table class="table table-bordered table-responsive">
                <thead>
                    <tr>
                        <td>ID</td>
                        <th>Title</th>
                        <th>Description</th>
                        <th>Created at</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>

/this function will fetch the datas coming from your database.

<?php 
                    if($blogs){
                        foreach($blogs as $blog){
                ?>
                    <tr>
                        <td><?php echo $blog->id; ?></td>
                        <td><?php echo $blog->title; ?></td>
                        <td><?php echo $blog->description; ?></td
                    </tr>
                <?php
                        }
                    }
                ?>
                </tbody>
            </table>

Solution

  • It's a CodeIgniter issue.

    For your problem solution use below view

    View:

    <?php
    if ($this->session->flashdata ( 'success' )) {
        ?>
        <div class="alert alert-success"> 
        <?php  echo $this->session->flashdata('success'); ?>
        <?php
        $this->session->unset_userdata ( 'success' );
    } else if ($this->session->flashdata ( 'error' )) {
        ?>
        <div class="alert alert-danger">
        <?php echo $this->session->flashdata('error'); ?>
        </div>
        <?php
        $this->session->unset_userdata ( 'error' );
    }
    ?>