Tuesday, July 12, 2022

[FIXED] How to show message on view in codeigniter?

Issue

i want to show the success message of a content update on a redirect link.here is my code of controller:-

public function add_content()
        {
        $this->load->helper('url');
    $id=$this->input->post('id');
        $content=$this->input->post('content');
        $title=$this->input->post('title');
        $this->load->model('admin/contentmodel');
    $status=$this->contentmodel->addcontent($id,$title,$content);
        if($status==1)
           {
            $this->session->set_flashdata("message","<font class='success'>Content Successfully Updated..!!</font>");
            redirect('admin/login/dash');
            }
        else
          {
           $this->session->set_flashdata("message","<font class='success'>Content Not Updated..!!</font>");
           redirect('admin/content/home');
          }
        }

my content is updated successfully and now i want to show the message to user on a particular redirect link.for that i set the message in above code:

$this->session->set_flashdata("message","<font class='success'>Content Successfully Updated..!!</font>");

so can you guys please let me know where i am going wrong and how can i show the error message on view?while my redirect goes to the controller ->than on view.so how can i flow my error MSG from controller->view.Thanks in advance.


Solution

Open application/config/config.php and edit the line:

$config['encryption_key'] = '';

by adding some random values to the string

$config['encryption_key'] = 'q0231sz!!1@asd';

After that, when you've set the message with

$this->session->set_flashdata('key', 'value');

in your view file simply echo

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

Note, that this will not echo your 'value' on this load, but will echo it after you refresh the page

$this->session->set_flashdata('mykey', 'testing');
echo $this->session->flashdata('mykey'); // will echo '' (nothing)


Answered By - Sergey Telshevsky
Answer Checked By - Mary Flores (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.