Harshal
Harshal

Reputation: 3622

How to show message on view in codeigniter?

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.

Upvotes: 3

Views: 19130

Answers (6)

Add following code to your controller/model :-

if($status==1)
{
     $data=array(
         'class' => 'success',
         'message' => 'Content Successfully Updated..!!'
     );
     $this->session->set_flashdata("dash",$data);
     redirect('admin/login/dash');
}
else
{
     $data=array(
         'class' => 'danger',
         'message' => 'Content Not Updated..!!'
     );
     $this->session->set_flashdata("dash",$data);
     redirect('admin/content/home');
}

Add following code to your view :-

if($this->session->has_userdata('dash'))
{
   echo '<div class="alert alert-'.$this->session->dash['class'].'">'.$this->session->dash['message'].'</div>';
}

Upvotes: 0

Siddharth Shukla
Siddharth Shukla

Reputation: 1131

**Set flash data in controller**

message - flash data session name. 

$this->session->set_flashdata('message', 'Sucessfully updated.');

Read flash data(Pass in view with bootstrap alert)

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

Upvotes: 0

yashan
yashan

Reputation: 177

You can set controller to,

$this->data['errormsg'] = "message";

then set view to

  echo isset($errormsg)?$errormsg:"" ;

Upvotes: 2

Sharad D
Sharad D

Reputation: 329

/* Do this in controller */

//This should have message you needed. Try logging in file for testing
$message = $this->session->flashdata("message");

$data["message"] = $message;

$this->load->view("yourview",$data);

/* Do this in your view */

echo $message;

Upvotes: 0

Jeff
Jeff

Reputation: 12163

The solution to your error is pointed out in your question:

In order to use the Session class you are required to set an encryption key in your config file.

Upvotes: 2

Sergey Telshevsky
Sergey Telshevsky

Reputation: 12197

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)

Upvotes: 7

Related Questions