0
votes

Well, this may be a silly question, but since I'm very new to codeigniter, I have no idea how to do this.

What I want to do is, display my custom errors messages whenever I want(such as login failed, Post Successfully published...etc.) in the front end (On login-form-view, write-post-view...etc.).

Earlier, when I was using just PHP (no frameworks), I stored those messages in a $_SESSION global variable and echo them in the front end pages. But here, I hope CodeIgniter comes with a better solution.

5
In codeigniter you can use $this->session->set_flashdata('success','Your message') for success and replace success with failure that's it - Narendrasingh Sisodia
Ok thanks, here failure means ? is it just a string ? - Tharindu Thisarasinghe
That's for differentiating those success and failure calls nothing else you can use your own - Narendrasingh Sisodia

5 Answers

2
votes

When you use session flash data it only works on redirect

For example

public function index() {
  $this->load->library('form_validation');

  $this->form_validation->set_rules('username', 'Username');
  $this->form_validation->set_rules('password', 'Password');

  if ($this->form_validation->run() == FALSE ) {

    $this->load->view('login');

  } else {

    $this->session->set_flashdata('success', 'You have logged on');
    redirect('success_controller');
  }
}

On Success Page View

<?php if ($this->session->flashdata('success')) { ?>
<?php echo $this->session->flashdata('success');?>
<?php }?>
2
votes

You can use flash data in codeigniter. It will clear automatically.

Controller :

//syntax => $this->session->set_flashdata('name', 'your message');

$this->session->set_flashdata('success', 'Post Successfully published.');

View :

<?php echo $this->session->flashdata('success');?>
1
votes

CodeIgniter has something called FlashData. Flash data will bring through a session variable that will exist for the next request, then clear itself after.

In CodeIgniter 3, you set flash data like this:

$this->session->mark_as_flash(array('item', 'item2'));

or:

$this->session->mark_as_flash('item');

Alternatively, you can use set_flashdata, which will work with previous versions of CodeIgniter:

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

You can view flashdata by doing this:

$this->session->flashdata('item');

If you want to preserve flash data through an additional request, you can use keep_flashdata:

$this->session->keep_flashdata('item');

FlashData uses the session library, so make sure it's loaded in either the library autoloader, or in the controller using it.

FlashData Documentation

1
votes

You can use $this->session->set_flashdata('item', 'value');

Refer http://www.codeigniter.com/userguide2/libraries/sessions.html

0
votes

In your Controller you can use like

$this->load->library('session');
$this->session->set_flashdata('msg', 'Your message here');
redirect('controller_name/method_name');      

In your View you can use like

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