Search code examples
phpcodeignitermessageform-submit

displaying success/error messages after form submission - codeigniter


I'm not able to know how to display the success / error messages. Main problem is where to put codes. Here, i'm inserting my code. Please help me to go through it. I'm new to codeigniter, here everything is in well organized. Before that, in core php, we pass error/success messages through header('location...'); but, here,it is totally different.

View (index.php)

<?php echo form_open_multipart('welcome/MemberFileUpload');?>
  <input type="file" name="files[]" multiple> <br>
  <input type='submit' value='Submit'>
</form>

Controller

class Welcome extends CI_Controller
{

    public function __construct()
        {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->library('session'); // Start Session
        $this->load->helper('form');
        $this->load->library('form_validation');
        }
 public function member_CAttachments()
 {
     $data['results'] = $this->news_model->member_MAttachments(); 
     $this->load->view('member/templates/header');
     $this->load->view('member/index',$data);
     $this->load->view('member/templates/footer');
    }
  function MemberFileUpload()
    {       
        $this->form_validation->set_rules('FileTitle', 'Title', 'required');
        if ($this->form_validation->run() === FALSE)
            {
            redirect('welcome/member_CAttachments/');
        }
        else
        {
                $FileTitle = $this->input->post('FileTitle');
                $FileDesc = $this->input->post('FileDesc');
            $CurrentDate=date("Y-m-d h:i:s");
            $InsertedFileID=$this->news_model->UploadFileDetails($FileDesc, $CurrentDate,$FileTitle);

            $UploadDirectory='assets/Upload/';
            $TotalUploadedFiles=count($_FILES['files']['name']);

            for($i=0;$i<$TotalUploadedFiles;$i++)
            {
                $UploadedFileName=$_FILES['files']['name'][$i];
                $EncFileName=time().$UploadedFileName;
                if(move_uploaded_file($_FILES['files']['tmp_name'][$i], $UploadDirectory.$EncFileName))
                    {
                    $this->news_model->UploadFiles($InsertedFileID, $EncFileName);
                } 
            }
            redirect('welcome/member_CAttachments/');
        }
     }
}

Model

class News_model extends CI_Model 
{

        public function __construct()
        {
                $this->load->database();
        }
        public function UploadFiles($InsertedFileID,$EncFileName)
        {
            $MemberData = array(
            'FileID' => $InsertedFileID,
            'FilePath' => $EncFileName
            );

            $this->db->insert('MemberFiles', $MemberData);
       }
}

Solution

  • add this to your controller:

    $this->session->set_flashdata('response',"Data Inserted Successfully");
    redirect('welcome/member_CAttachments/');
    

    add this to your view:

    echo $this->session->flashdata('response');