Codeigniter 3 flashdata bootstrap message not showing although data added to database

Making a simple codeigniter 3 web application where i wanted to add category name to the database through a form but even after submit the set flashdata msg not showing on front end…although i can see data added to table in phpmyadmin

here is my code views list.php

<?php $this->load->view('admin/header'); ?>

    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark">Categories</h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active">Categories</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
 <!-- Main content -->
    <div class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-12">
               <?php 
      if($this->session->flashdata('success') !=  ""){ ?>
         <div class='alert alert-success mb-3'><?php echo $this->session->flashdata('success');?></div>;
    <?php  }
       ?>
              
             <!--<div class="alert alert-success">success</div>-->
            <div class="card">
               <!--<div class="card-body d-flex justify-content-center align-items-center" style="height:450px;">
                <h3 class="">Welcome to Codeigniter Web Console</h5>
              </div>-->
              <div class="card-header">
                 <div class="card-title">
                     <form id="SearchFrm" name="searchFrm" action="" method="">
                          <div class="input-group mb-0">
                              <input type="text" value="" class="form-control" placeholder="Search" id="" name="q">
                              <div class="input-group-append">
                                <button class="input-group-text"><i class="fas fa-search"></i></button>
                              </div>
                         </div>
                     </form>
                    
                 </div><!--card-title-->
                 <div class="card-tools">
                     <a href="<?php echo base_url().'admin/category/create' ?>" class="btn btn-primary"><i class="fas fa-plus"></i> Create </a>
                 </div>
              </div><!--card-header-->
              
              <div class="card-body">
                  <table class="table table-striped">
                    <thead>
                      <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th width="100">Status</th>
                        <th width="160" class="text-center">Action</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr>
                        <td>1</td>
                        <td>Fashion</td>
                        <td><span class="badge badge-success">Staus</span></td>
                        <td class="text-center"><a href="" class="btn btn-sm  btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
                      </tr>
                      <tr>
                        <td>1</td>
                        <td>Fashion</td>
                        <td><span class="badge badge-success">Staus</span></td>
                        <td class="text-center"><a href="" class="btn btn-sm  btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
                      </tr>
                      <tr>
                        <td>1</td>
                        <td>Fashion</td>
                        <td><span class="badge badge-success">Staus</span></td>
                        <td class="text-center"><a href="" class="btn btn-sm  btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
                      </tr>
                     
                    </tbody>
                 </table>
              </div><!--card-body-->
              
            </div>
          </div>
          <!-- /.col-md-6 -->
       
        </div>
        <!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

<?php $this->load->view('admin/footer'); ?>
 

 

i even tried this code

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

but that didnt work too

Update as dontpanic said i have updated my code Category.php(controller)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Category extends CI_Controller {

   
    //this method will show category list page
    public function index()
    {
     $this->load->view('admin/category/list');
    }
    
    //this method will show create category page
    public function create()
    {
     $this->load->model('Category_model');
     $this->load->library('form_validation');
      $this->form_validation->set_error_delimiters(' <p class="text-danger">','</p>');
      $this->form_validation->set_rules('name','Name','trim|required');

      if($this->form_validation->run() == true){
          //will check category in database
          $formArray['name'] = $this->input->post('name');
          $formArray['status'] = $this->input->post('status');
          $formArray['created_at'] = date('Y-m-d H:i:s');
          $this->Category_model->create($formArray);
          
          $this->session->flashdata('success','Category added successfully');
          redirect (base_url().'admin/category/index');
          
      }else{
          //Will show errors
          $this->load->view('admin/category/create');  
      }
    
    // $this->load->view('admin/category/create');  
    }
    
   //this method will show edit category page
    public function edit()
    {
        
    }
    
    //this method will will  delete a category 
    public function delete()
    {
        
    }
}

Category_model.php (model)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Category_model extends CI_Model {
    
    public function create($formArray){
      $this->db->insert('categories',$formArray);
    
  }
   
}

and updated list.php(after tips from vansh patel)(views)

<?php $this->load->view('admin/header'); ?>

    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark">Categories</h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active">Categories</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
 <!-- Main content -->
    <div class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-12">
               <?php 
               $flashdata = $this->session->flashdata('success');
      if($flashdata !=  ""){ ?>
         <div class='alert alert-success mb-3'><?php echo $this->session->flashdata('success');?></div>;
    <?php  }
       ?>
              
             <!--<div class="alert alert-success">success</div>-->
            <div class="card">
               <!--<div class="card-body d-flex justify-content-center align-items-center" style="height:450px;">
                <h3 class="">Welcome to Codeigniter Web Console</h5>
              </div>-->
              <div class="card-header">
                 <div class="card-title">
                     <form id="SearchFrm" name="searchFrm" action="" method="">
                          <div class="input-group mb-0">
                              <input type="text" value="" class="form-control" placeholder="Search" id="" name="q">
                              <div class="input-group-append">
                                <button class="input-group-text"><i class="fas fa-search"></i></button>
                              </div>
                         </div>
                     </form>
                    
                 </div><!--card-title-->
                 <div class="card-tools">
                     <a href="<?php echo base_url().'admin/category/create' ?>" class="btn btn-primary"><i class="fas fa-plus"></i> Create </a>
                 </div>
              </div><!--card-header-->
              
              <div class="card-body">
                  <table class="table table-striped">
                    <thead>
                      <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th width="100">Status</th>
                        <th width="160" class="text-center">Action</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr>
                        <td>1</td>
                        <td>Fashion</td>
                        <td><span class="badge badge-success">Staus</span></td>
                        <td class="text-center"><a href="" class="btn btn-sm  btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
                      </tr>
                      <tr>
                        <td>1</td>
                        <td>Fashion</td>
                        <td><span class="badge badge-success">Staus</span></td>
                        <td class="text-center"><a href="" class="btn btn-sm  btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
                      </tr>
                      <tr>
                        <td>1</td>
                        <td>Fashion</td>
                        <td><span class="badge badge-success">Staus</span></td>
                        <td class="text-center"><a href="" class="btn btn-sm  btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
                      </tr>
                     
                    </tbody>
                 </table>
              </div><!--card-body-->
              
            </div>
          </div>
          <!-- /.col-md-6 -->
       
        </div>
        <!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

<?php $this->load->view('admin/footer'); ?>
 

create.php (views)(where form for adding category is present)

<?php $this->load->view('admin/header'); ?>

    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark">Categories</h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item"><a href="<?php echo base_url().'admin/category/index' ?>">Categories</a></li>
              <li class="breadcrumb-item active">Create New Category</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
 <!-- Main content -->
    <div class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-12">
            <div class="card card-primary">
               <!--<div class="card-body d-flex justify-content-center align-items-center" style="height:450px;">
                <h3 class="">Welcome to Codeigniter Web Console</h5>
              </div>-->
              <div class="card-header">
                 <div class="card-title">
                     Create New Category
                  </div><!--card-title-->
              </div><!--card-header-->
              <form name="categoryForm" id="categoryForm" method="post" action="<?php echo base_url().'admin/category/create' ?>">
                  <div class="card-body">
                      
                          <div class="form-group">
                            <label>Name</label>
                            <input type="text" class="form-control <?php echo( form_error('name') != "") ? 'is-invalid' : ''; ?>" value="" name="name" id="name">
                            
                          <?php echo form_error('name');?>
  
                           
                          </div>
                          
                          <div class="form-group">
                            <label>Image</label>
                            <input type="file" class="form-control-file border" name="image" id="image">
                          </div>
                          
                          <div class="form-group row">
                              <div class="col-sm-1 col-xs-1">
                                  <div class="custom-control custom-radio">
                                      <input class="custom-control-input" type="radio" value="1" id="statusActive" name="status" checked>
                                      <label for="statusActive" class="custom-control-label">Active</label>
                                   </div>
                              </div>
                              <div class="col-sm-1 col-xs-1">
                                   <div class="custom-control custom-radio ml-3">
                                      <input class="custom-control-input" type="radio" value="0" id="statusBlock" name="status">
                                      <label for="statusBlock" class="custom-control-label">Block</label>
                                  </div>
                              </div>
                              
                          </div>
                      
                           <!--<div class="custom-control custom-radio float-left">
                              <input class="custom-control-input" type="radio" value="1" id="statusActive" name="statusActive" checked>
                              <label for="statusActive" class="custom-control-label">Active</label>
                            </div>
                            <div class="custom-control custom-radio ml-3">
                              <input class="custom-control-input" type="radio" value="1" id="statusBlock" name="statusBlock">
                              <label for="statusBlock" class="custom-control-label">Block</label>
                            </div>-->
                         
                     
                  </div><!--card-body-->
              
                  <div class="card-footer">
                      <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                      <a href="<?php echo base_url().'admin/category/index' ?>" class="btn btn-secondary">Back</a>
                  </div><!--card-footer-->
             </form>
            </div>
          </div>
          <!-- /.col-md-6 -->
       
        </div>
        <!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

<?php $this->load->view('admin/footer'); ?>
 

The issue you’re experiencing with the flash message not displaying might be due to an incorrect use of the set_flashdata function in your controller. Specifically, in your create method of the Category controller, you’re using:

$this->session->flashdata('success', 'Category added successfully');

This is incorrect because flashdata is used to retrieve flash data, not to set it. Instead, you should use set_flashdata to set the flash message. Here’s how to fix your code:

Updated create Method in Category.php (Controller)

public function create()
{
    $this->load->model('Category_model');
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
    $this->form_validation->set_rules('name', 'Name', 'trim|required');

    if ($this->form_validation->run() == true) {
        // Save category to database
        $formArray['name'] = $this->input->post('name');
        $formArray['status'] = $this->input->post('status');
        $formArray['created_at'] = date('Y-m-d H:i:s');
        $this->Category_model->create($formArray);

        // Set flashdata
        $this->session->set_flashdata('success', 'Category added successfully');
        redirect(base_url() . 'admin/category/index');
    } else {
        // Show form with validation errors
        $this->load->view('admin/category/create');
    }
}

Explanation of Fix

  1. Correct Flashdata Setting: Replace $this->session->flashdata('success', 'Category added successfully'); with $this->session->set_flashdata('success', 'Category added successfully');. This ensures the success message is stored in the session for one request cycle.
  2. Redirect After Success: After setting the flash message, redirect to the desired page (e.g., the category list).

Displaying Flashdata in list.php (View)

Ensure you’re checking and displaying the flash data correctly:

<?php if ($this->session->flashdata('success')): ?>
    <div class="alert alert-success mb-3">
        <?php echo $this->session->flashdata('success'); ?>
    </div>
<?php endif; ?>

Important Notes

  1. Load Session Library: Ensure that the Session library is loaded. It can be autoloaded by adding it to autoload.php:
$autoload['libraries'] = array('database', 'session');
  1. Redirect Behavior: Flash data only persists for the next request, so you must use a redirect to ensure it’s displayed.
  2. Form Validation Errors: If the form validation fails, the flash data won’t be set or displayed, and the user will remain on the create page with validation error messages.

Debugging Checklist

  • Confirm that the session is properly configured in your CodeIgniter setup.
  • Test the flash message by temporarily printing it in the controller after setting it:
$this->session->set_flashdata('success', 'Test Message');
echo $this->session->flashdata('success');

This should resolve your issue with the flash message not displaying. Let me know if you need further assistance!