skip to Main Content

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

2

Answers


  1. The if condition validates the flashdata message action as true and would be visible in the view after the redirection, therefore the code would be like this

    <?php if($this->session->flashdata('success')): ?>
    
            <div class="alert alert-success">
            <?php echo $this->session->flashdata('success'); ?>
          </div>
    
    <?php endif; ?>
    
    Login or Signup to reply.
  2. The problem seems to be how you set your CodeIgniter flashdata In your Category.php controller, instead of using

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

    try using this

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

    and then in the view file use flashdata to retrieve and display flash data

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search