Flash message show if data inserted

In this tutorial, we are going to create flash message show if data is inserted in database.

eg. If you create a login and user enters the wrong username and password  so it will show the error message.

 

What is Flashdata

You can perform different session message depends what you you pass to view file from your controller.

 

Step 1 : Go you you dashboard controller and  there we willl put the flash message code when our user data is inserted.

eg : 

$this->session->set_flashdata('msg','User data has been inserted successfully!');
$this->session->set_flashdata('msg_class','alert-success');    

 

Complete code of insert user detail.

public function add_user_detail(){

	$this->load->helper('form');
	$this->load->library('form_validation');
	$this->form_validation->set_rules('name','Full Name','required');
	$this->form_validation->set_rules('email','Email','required');
	$this->form_validation->set_rules('contact','Contact','required');
	$this->form_validation->set_rules('status','Email','required');
	if($this->form_validation->run())
	{
		$data = array(
			'username' => $this->input->post('name'),
			'email' => $this->input->post('email'),
			'contact' => $this->input->post('contact'),
			'status' => $this->input->post('status'), 
		);

		$this->load->model('dashboard_model','userdata');

		if($this->userdata->add_user($data))
		{
			//here i am creating a success flash message
			$this->session->set_flashdata('msg','User data has been inserted successfully!');
			$this->session->set_flashdata('msg_class','alert-success');	
		}
		else{
			$this->session->set_flashdata('msg','User data has not been inserted successfully!');
			$this->session->set_flashdata('msg_class','alert-danger');	
		}
		return redirect('dashboard');
	}
	else{
		$this->load->view('add_user_detail');
	}
	
}

 

Step 2 :  Now, pass the flash msg on user list view page..

eg : 

<?php
    if($msg = $this->session->flashdata('msg'))
    {
        $msg_class = $this->session->flashdata('msg_class');
?>

    <div class="alert <?php echo $msg_class; ?>">
      <strong><?php echo $msg; ?></strong>
    </div>

<?php 
    }
?>


Result :

Source Code:

Codeigniter tutorial for beginners

In this project. We are providing you, how to create small project in Codeign....

Source Code